-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay9.java
More file actions
35 lines (29 loc) · 974 Bytes
/
Day9.java
File metadata and controls
35 lines (29 loc) · 974 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
// 1291. Sequential Digits
// An integer has sequential digits if and only if each digit in the number is one more than the previous digit.
// Return a sorted list of all the integers in the range [low, high] inclusive that have sequential digits.
// Example 1:
// Input: low = 100, high = 300
// Output: [123,234]
// Example 2:
// Input: low = 1000, high = 13000
// Output: [1234,2345,3456,4567,5678,6789,12345]
// Constraints:
// 10 <= low <= high <= 10^9
// Solution
class Solution {
public List<Integer> sequentialDigits(int low, int high) {
List<Integer> ans = new ArrayList<>();
Queue<Integer> q = new ArrayDeque<>(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9));
while (!q.isEmpty()) {
final int num = q.poll();
if (num > high)
return ans;
if (low <= num && num <= high)
ans.add(num);
final int lastDigit = num % 10;
if (lastDigit < 9)
q.offer(num * 10 + lastDigit + 1);
}
return ans;
}
}