-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path967.cpp
29 lines (29 loc) · 810 Bytes
/
967.cpp
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
class Solution {
public:
vector<int> res;
void backtracking(string& num, int current, int n, int k) {
if (current == n) {
res.push_back(stoi(num));
return;
}
int last = num.back() - '0';
if (last + k <= 9) {
num.push_back(last + k + '0');
backtracking(num, current + 1, n, k);
num.pop_back();
}
if (k != 0 && last - k >= 0) {
num.push_back(last - k + '0');
backtracking(num, current + 1, n, k);
num.pop_back();
}
}
vector<int> numsSameConsecDiff(int n, int k) {
for (int i = 1; i < 10; ++i) {
string temp;
temp.push_back(i + '0');
backtracking(temp, 1, n, k);
}
return res;
}
};