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 List<String> summaryRanges(int[] nums) {
List<String> ranges = new ArrayList<String>();
if (nums.length == 0) return ranges;
int start = 0;
int end = 0;
for (int i = 0; i < nums.length; i++) {
if (i + 1 < nums.length && nums[i] == nums[i + 1] - 1) end++;
else {
if (start == end) ranges.add(String.valueOf(nums[start]));
else ranges.add(nums[start] + "->" + nums[end]);
start = ++end;
}
}
return ranges;
}