05 August 2008
Given a sorted integer array without duplicates, return the summary of its ranges.
Example 1:
1
2
3
Input: [0,1,2,4,5,7]
Output: ["0->2","4->5","7"]
Explanation: 0,1,2 form a continuous range; 4,5 form a continuous range.
Example 2:
1
2
3
Input: [0,2,3,4,6,8,9]
Output: ["0","2->4","6","8->9"]
Explanation: 2,3,4 form a continuous range; 8,9 form a continuous range.
这道题的思路比较简单,从左边找到右边即可,注意判断一下如果是连续的元素就用”->“,否则就是元素自己,注意下边界,在for循环中,注意每次i在循环中是一段连续数字移动到终止地方的位置,同时每次for循环i还会自增1。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Solution {
public List<String> summaryRanges(int[] nums) {
List<String> result = new ArrayList<>();
int len = nums.length;
for (int i = 0; i < len; i++) {
int temp = nums[i];
while (i + 1 < len && nums[i + 1] - nums[i] == 1) {//是连续元素
i++;
}
if (nums[i] != temp) {//多于一个元素
result.add(temp + "->" + nums[i]);
} else {//只有当前元素自己
result.add(nums[i] + "");
}
}
return result;
}
}