-
Notifications
You must be signed in to change notification settings - Fork 5
/
ContiguouslyIncreasingNumbers.java
67 lines (53 loc) · 1.61 KB
/
ContiguouslyIncreasingNumbers.java
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*https://binarysearch.com/problems/Contiguously-Increasing-Numbers*/
import java.util.*;
class Solution {
public int[] solve(int start, int end) {
Queue<Integer> queue = new LinkedList<Integer>();
ArrayList<Integer> result = new ArrayList<Integer>();
int i, dig;
String temp;
for (i = 1; i <= 9; ++i)
{
queue.add(i);
if (i >= start && i <= end) result.add(i);
}
while (!queue.isEmpty())
{
i = (Integer)queue.poll();
dig = i%10;
++dig;
if (dig > 9) continue;
i *= 10;
i += dig;
if (i >= start && i <= end)
result.add(i);
if (i <= end) queue.add(i);
}
int[] arr = new int[result.size()];
for (i = 0; i < arr.length; ++i)
arr[i] = (Integer)result.get(i);
return arr;
}
}
import java.util.*;
class Solution {
public int[] solve(int start, int end) {
Queue<Integer> queue = new LinkedList<Integer>();
ArrayList<Integer> result = new ArrayList<Integer>();
int i, dig;
String temp;
for (i = 1; i <= 9; ++i) queue.add(i);
while (!queue.isEmpty())
{
i = (Integer)queue.poll();
if (i >= start && i <= end) result.add(i);
if (i%10 == 9) continue;
i = (i*10)+((i%10)+1);
if (i <= end) queue.add(i);
}
int[] arr = new int[result.size()];
for (i = 0; i < arr.length; ++i)
arr[i] = (Integer)result.get(i);
return arr;
}
}