LeetCode 1291. 顺次数

时间:2019-12-19
本文章向大家介绍LeetCode 1291. 顺次数,主要包括LeetCode 1291. 顺次数使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

地址 https://leetcode-cn.com/problems/sequential-digits/submissions/

题目描述
我们定义「顺次数」为:每一位上的数字都比前一位上的数字大 1 的整数。

请你返回由 [low, high] 范围内所有顺次数组成的 有序 列表(从小到大排序)。

示例 1:

输出:low = 100, high = 300
输出:[123,234]
示例 2:

输出:low = 1000, high = 13000
输出:[1234,2345,3456,4567,5678,6789,12345]
 

提示:

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

算法1
考虑到最多就是1~9 就提前用DFS写了个表
执行代码的时候直接查表

C++ 代码

class Solution {
public:
    vector<int> v = {
1,
2,
3,
4,
5,
6,
7,
8,
9,
12,
23,
34,
45,
56,
67,
78,
89,
123,
234,
345,
456,
567,
678,
789,
1234,
2345,
3456,
4567,
5678,
6789,
12345,
23456,
34567,
45678,
56789,
123456,
234567,
345678,
456789,
1234567,
2345678,
3456789,
12345678,
23456789,
123456789,
};
vector<int> ret;
    vector<int> sequentialDigits(int low, int high) {
        for(int i = 0; i < v.size();i++){
            if(v[i]>=low && v[i] <= high){
                ret.push_back(v[i]);
            }

        }

        return ret;
    }
};

原文地址:https://www.cnblogs.com/itdef/p/12066088.html