leetcode

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

1291.cpp (980B)


      1 class Solution {
      2   public:
      3     vector<int> sequentialDigits(const int low, const int high) {
      4         vector<int> res;
      5         for (int i = 1; i <= 9; i++) {
      6             int crnt = i;
      7             for (int j = i + 1; j <= 9; j++) {
      8                 crnt = crnt * 10 + j;
      9                 if (crnt > high) break;
     10                 if (crnt >= low) res.push_back(crnt);
     11             }
     12         }
     13         sort(begin(res), end(res));
     14         return res;
     15     }
     16 };
     17 
     18 // BFS, without sort
     19 class Solution {
     20   public:
     21     vector<int> sequentialDigits(const int low, const int high) {
     22         queue<int> q;
     23         vector<int> res;
     24         for (int i = 1; i <= 9; i++)
     25             q.push(i);
     26         while (!q.empty()) {
     27             const int crnt = q.front();
     28             q.pop();
     29             if (crnt > high) continue;
     30             if (crnt >= low) res.push_back(crnt);
     31             if (crnt % 10 == 9) continue;
     32             q.push(crnt * 10 + crnt % 10 + 1);
     33         }
     34         return res;
     35     }
     36 };