leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

0967.cpp (837B)


0 class Solution {
1 public:
2 vector<int> numsSameConsecDiff(const int n, const int k) const {
3 static const int lookup[] = {0, 1, 10, 100, 1000, 10000,
4 100000, 1000000, 10000000, 100000000, 1000000000};
5 vector<int> res;
6 queue<int> q;
7 for (int i = 1; i <= 9; i++)
8 q.push(i);
9 while (!q.empty()) {
10 const int crnt = q.front();
11 q.pop();
12 if (crnt >= lookup[n])
13 res.push_back(crnt);
14 else {
15 const int low = crnt % 10 - k, high = crnt % 10 + k;
16 if (high <= 9) q.push(crnt * 10 + high);
17 if (k == 0) continue;
18 if (low >= 0) q.push(crnt * 10 + low);
19 }
20 }
21 return res;
22 }
23 };