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)


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