-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSummaryRanges.java
More file actions
43 lines (31 loc) · 1018 Bytes
/
Copy pathSummaryRanges.java
File metadata and controls
43 lines (31 loc) · 1018 Bytes
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
31
32
33
34
35
36
37
38
39
40
41
42
43
class Solution {
/**
* url: https://leetcode.com/problems/summary-ranges/
* Things to remember:
- convert integer to string
Integer.toString(number)
*/
public List<String> summaryRanges(int[] nums) {
if (nums.length < 1) {
return new ArrayList<>();
}
int prev = 0 ;
int next = prev + 1;
List<String> output = new ArrayList<>();
for( int i = 0; i <= nums.length - 1; i++) {
if (next < nums.length && nums[next] - nums[i] == 1) {
next++;
} else {
if ((next - 1) == prev) {
output.add(Integer.toString(nums[prev]));
} else {
String tempOutput = Integer.toString(nums[prev]) + "->" + Integer.toString(nums[next - 1]);
output.add(tempOutput);
}
prev = i + 1;
next = prev + 1;
}
}
return output;
}
}