-
Notifications
You must be signed in to change notification settings - Fork 0
/
S228.java
30 lines (27 loc) · 1.02 KB
/
S228.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import java.util.ArrayList;
import java.util.List;
/**
* LC#228 汇总区间 Summary Ranges
* Link:https://leetcode-cn.com/problems/summary-ranges/
* 思路:2层循环,外层循环每次计算去区间的 low...hign,然后通过 string 构建字符连接,放入集合
*/
public class S228 {
public List<String> summaryRanges(int[] nums) {
List<String> ret = new ArrayList<>();
int i = 0;
while (i < nums.length) {
int low = i; i++;
while (i < nums.length && nums[i] == nums[i - 1] + 1) i++;
StringBuffer temp = new StringBuffer(Integer.toString(nums[low]));
if (low < i - 1) temp.append("->").append(Integer.toString(nums[i - 1]));
ret.add(temp.toString());
}
return ret;
}
public static void main(String[] args) {
int[] nums = { 0, 1, 2, 4, 5, 7 };
S228 solution228 = new S228();
List<String> summaryRanges = solution228.summaryRanges(nums);
System.out.println(summaryRanges);
}
}