public class Solution {
public List<String> summaryRanges(int[] nums) {
List<String> res = new ArrayList<>();
if (nums == null || nums.length < 1) return res;
int len = nums.length;
int i = 0;
while (i < len) {
StringBuilder sb = new StringBuilder();
int start = nums[i];
sb.append(nums[i++]);
while (i < len && nums[i] == nums[i - 1] + 1) {
i++;
}
if (start != nums[i - 1]) {
sb.append("->").append(nums[i - 1]);
}
res.add(sb.toString());
}
return res;
}
}
public class Solution {
public List<String> summaryRanges(int[] nums) {
List<String> res = new ArrayList<>();
if (nums.length == 0) return res;
if (nums.length == 1) {
res.add(nums[0] + "");
return res;
}
int start = nums[0];
int prev = nums[0];
for (int i = 1; i < nums.length; i++) {
int cur = nums[i];
if (prev + 1 != cur) {
// End
res.add(start != prev ? start + "->" + prev : start + "");
// New start
start = cur;
}
prev = cur;
}
res.add(start != prev ? start + "->" + prev : start + "");
return res;
}
}