题目:
题解:
class Solution {
public List<String> summaryRanges(int[] nums) {
List<String> ans = new ArrayList<>();
for (int i = 0, j, n = nums.length; i < n; i = j + 1) {
j = i;
while (j + 1 < n && nums[j + 1] == nums[j] + 1) {
++j;
}
ans.add(f(nums, i, j));
}
return ans;
}
private String f(int[] nums, int i, int j) {
return i == j ? nums[i] + "" : String.format("%d->%d", nums[i], nums[j]);
}
}