summaryRanges
T: O(n)
S: OI(1)
class Solution {
public List<String> summaryRanges(int[] nums) {
List<String> res = new ArrayList<>();
int n = nums.length;
if (nums == null || n == 0) {
return res;
}
for (int i = 0; i < n; i++) {
int start = nums[i];
while (i < n-1 && nums[i]+1 == nums[i+1]) {
i++;
}
if (start == nums[i]) {
res.add(String.valueOf(start));
} else {
res.add(start + "->" + nums[i]);
}
}
return res;
}
}
/*
use two pointers, T: O(n), S: O(1)
start = nums[i];
while (i < n-1 && nums[i]+1 == nums[i+1]) {
i++;
}
if ()
s. i
0 1 2 4 5 7
0->2
s i
0 1 2 4 5 7
4 -> 5
i
s
0 1 2 4 5 7
7
*/
Last updated