An integer has sequential digits if and only if each digit in the number is one more than the previous digit.

Return a sorted list of all the integers in the range [low, high] inclusive that have sequential digits.

输入格式

数据范围

  • 10 <= low <= high <= 10^9

There are two integers low and high that represent the range.

输出格式

Output a list of integers.

输入样例:

low = 100, high = 300

输出样例:

[123,234]

输入样例:

low = 1000, high = 13000

输出样例:

[1234,2345,3456,4567,5678,6789,12345]

题目分析

DFS

代码实现

class Solution {
public:
    vector <int> ans;
    void dfs(int num, int l, int r) {
        if(num >= l && num <= r) ans.push_back(num);
        if(num > r) return ;
        if(num
    }
    vector<int> sequentialDigits(int low, int high) {
        for(int i = 1; i <= 9; ++i) dfs(i, low, high);
        sort(ans.begin(), ans.end());
        return ans;
    }
};
分类: DFSRecursion

0 条评论

发表回复

Avatar placeholder

您的电子邮箱地址不会被公开。 必填项已用*标注

友情链接:Ctips' blog, Colza’s blog

站点状态:Status