1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| class Solution { public List<String> summaryRanges(int[] nums) { List<String> resultList = new ArrayList<>();
int i = 0; while (i < nums.length) { int j = i + 1; while (j < nums.length && nums[j] == nums[j - 1] + 1) { j++; }
int end = j - 1; if (end == i) { resultList.add(String.valueOf(nums[i])); } else { resultList.add(nums[i] + "->" + nums[end]); } i = j; }
return resultList; } }
|