1291. Sequential Digits
https://leetcode.com/problems/sequential-digits/
按序找到[low, high]内所有digit满足递增的数。这样的数在整数范围内一共也就常数个,可以都列出来。也可以用BFS或string操作的方式把所有的可能遍历出来。
class Solution {
public:
vector<int> sequentialDigits(int low, int high) {
queue<int> q;
for (int i = 1; i <= 9; ++i) q.push(i);
vector<int> res;
while (!q.empty()) {
auto t = q.front(); q.pop();
if (t > high) break;
if (t >= low && t <= high) {
res.push_back(t);
}
if (t % 10 == 9) continue;
q.push(t * 10 + t % 10 + 1);
}
return res;
}
};
Last updated
Was this helpful?