-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathsolution.java
More file actions
32 lines (24 loc) · 977 Bytes
/
Copy pathsolution.java
File metadata and controls
32 lines (24 loc) · 977 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
class Solution {
public List<Integer> sequentialDigits(int low, int high) {
// String containing all consecutive digits
String digits = "123456789";
// Result list
List<Integer> ans = new ArrayList<>();
// Number of digits in low and high
int minLen = String.valueOf(low).length();
int maxLen = String.valueOf(high).length();
// Try every possible length
for (int len = minLen; len <= maxLen; len++) {
// Generate every substring of current length
for (int start = 0; start + len <= 9; start++) {
// Convert substring into an integer
int num = Integer.parseInt(digits.substring(start, start + len));
// Keep only numbers inside the range
if (num >= low && num <= high) {
ans.add(num);
}
}
}
return ans;
}
}