leetcodeSolution to some Leetcode problems written in C++ |
git clone git://git.dimitrijedobrota.com/leetcode.git |
Log | Files | Refs | README | LICENSE |
1291.cpp (980B)
0 class Solution { 1 public: 2 vector<int> sequentialDigits(const int low, const int high) { 3 vector<int> res; 4 for (int i = 1; i <= 9; i++) { 5 int crnt = i; 6 for (int j = i + 1; j <= 9; j++) { 7 crnt = crnt * 10 + j; 8 if (crnt > high) break; 9 if (crnt >= low) res.push_back(crnt); 10 } 11 } 12 sort(begin(res), end(res)); 13 return res; 14 } 15 }; 16 17 // BFS, without sort 18 class Solution { 19 public: 20 vector<int> sequentialDigits(const int low, const int high) { 21 queue<int> q; 22 vector<int> res; 23 for (int i = 1; i <= 9; i++) 24 q.push(i); 25 while (!q.empty()) { 26 const int crnt = q.front(); 27 q.pop(); 28 if (crnt > high) continue; 29 if (crnt >= low) res.push_back(crnt); 30 if (crnt % 10 == 9) continue; 31 q.push(crnt * 10 + crnt % 10 + 1); 32 } 33 return res; 34 } 35 };