-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathNumbersWithSameConsecutiveDifferences.java
147 lines (143 loc) · 4.05 KB
/
NumbersWithSameConsecutiveDifferences.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*https://leetcode.com/problems/numbers-with-same-consecutive-differences/*/
//Backtracking
class Solution {
ArrayList<Integer> result;
public int[] numsSameConsecDiff(int n, int k) {
result = new ArrayList<Integer>();
for (int dig = 1; dig <= 9; ++dig)
constructNumber(n-1,k,dig, new StringBuilder(Integer.toString(dig)));
int[] arr = new int[result.size()];
int i = 0;
for (Integer val : result)
arr[i++] = val;
return arr;
}
private void constructNumber(int n, int k, int digit, StringBuilder number)
{
if (n == 0)
{
result.add(Integer.parseInt(number.toString()));
return;
}
int next = digit+k;
if (next <= 9)
{
number.append(next);
constructNumber(n-1, k, next, number);
number.delete(number.length()-1,number.length());
}
if (digit-k != next)
{
next = digit-k;
if (next >= 0)
{
number.append(next);
constructNumber(n-1, k, next, number);
number.delete(number.length()-1,number.length());
}
}
}
}
class Solution {
ArrayList<Integer> result;
public int[] numsSameConsecDiff(int n, int k) {
result = new ArrayList<Integer>();
for (int dig = 1; dig <= 9; ++dig)
constructNumber(n-1,k,dig,dig);
int[] arr = new int[result.size()];
int i = 0;
for (Integer val : result)
arr[i++] = val;
return arr;
}
private void constructNumber(int n, int k, int digit, int number)
{
if (n == 0)
{
result.add(number);
return;
}
int next = digit+k;
if (next <= 9)
{
number = number*10+next;
constructNumber(n-1, k, next, number);
number /= 10;
}
if (digit-k != next)
{
next = digit-k;
if (next >= 0)
{
number = number*10+next;
constructNumber(n-1, k, next, number);
number /= 10;
}
}
}
}
class Solution {
ArrayList<Integer> result;
public int[] numsSameConsecDiff(int n, int k) {
result = new ArrayList<Integer>();
for (int dig = 1; dig <= 9; ++dig)
constructNumber(n-1,k,dig,dig);
int[] arr = new int[result.size()];
int i = 0;
for (Integer val : result)
arr[i++] = val;
return arr;
}
private void constructNumber(int n, int k, int digit, int number)
{
if (n == 0)
{
result.add(number);
return;
}
int next = digit+k;
if (next <= 9)
constructNumber(n-1, k, next, number*10+next);
if (digit-k != next)
{
next = digit-k;
if (next >= 0)
constructNumber(n-1, k, next, number*10+next);
}
}
}
//BFS
class Solution {
ArrayList<Integer> result;
public int[] numsSameConsecDiff(int n, int k) {
result = new ArrayList<Integer>();
int number, numLen = 1, queueLen = 0, prevDig, nextDig;
Queue<Integer> queue = new LinkedList<Integer>();
for (int dig = 1; dig <= 9; ++dig)
queue.add(dig);
while (numLen < n)
{
++numLen;
queueLen = queue.size();
while (queueLen-- > 0)
{
number = queue.poll();
prevDig = number%10;
nextDig = prevDig+k;
if (nextDig <= 9)
queue.add(number*10+nextDig);
if (nextDig != prevDig-k)
{
nextDig = prevDig-k;
if (nextDig >= 0)
queue.add(number*10+nextDig);
}
}
}
int[] arr = new int[queue.size()];
int i = 0;
while (!queue.isEmpty())
arr[i++] = queue.poll();
return arr;
}
}