# LeetCode-228.Summary Ranges

https://leetcode.com/problems/summary-ranges/

Given a sorted integer array without duplicates, return the summary of its ranges.

For example, given [0,1,2,4,5,7], return ["0->2","4->5","7"].

public IList<string> SummaryRanges(int[] nums)
{
int start, precur, cur = -1, n = nums.Length, left = 0;
IList<string> res = new List<string>();
while (cur != n - 1)
{
start = cur + 1;
precur = n - 1;
cur = precur;
while (nums[cur] - nums[start] != cur - start)
{
precur = cur;
cur = (start + cur) / 2;
}
if (cur == n - 1)
{
if (cur == start)
res.Add(nums[cur].ToString());
else
res.Add(nums[start] + "->" + nums[cur]);
break;
}
while (left + 1 != precur)
{
if (nums[cur] - nums[start] == cur - start)
{
left = cur;
cur = (precur + cur) / 2;
}
else
{
precur = cur;
cur = (left + cur) / 2;
}
}
if (cur == start)
res.Add(nums[cur].ToString());
else
res.Add(nums[start] + "->" + nums[cur]);
}
return res;
}

public IList<string> SummaryRanges(int[] nums)
{
int start = 0, end = 0, n = nums.Length;
IList<string> res = new List<string>();
while (end < n)
{
if (end + 1 < n && nums[end + 1] - nums[end] == 1)
end++;
else
{
if (start == end)
res.Add(nums[end].ToString());
else
res.Add(nums[start] + "->" + nums[end]);
start = end = end + 1;
}
}
return res;
}

• 本文已收录于以下专栏：

举报原因： 您举报文章：LeetCode-228.Summary Ranges 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)