Loading

【leetcode】1291. Sequential Digits

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.

 

Example 1:

Input: low = 100, high = 300
Output: [123,234]

Example 2:

Input: low = 1000, high = 13000
Output: [1234,2345,3456,4567,5678,6789,12345

纯纯暴力法,代码扩展性差。。 但速度真的快

class Solution {
public:
    vector<int> sequentialDigits(int low, int high) {
        int beginer[9]={12,123,1234,12345,123456,1234567,12345678,123456789};
        int addtot[9]={11,111,1111,11111,111111,1111111,11111111,111111111};
        vector<int>res={};
        int low_bit=1;
        int high_bit=1;
        int low_=low,high_=high;
        while((low_/=10)) low_bit++;
        while((high_/=10)) high_bit++;
        for(int i=low_bit-2;i<=high_bit-2;++i){
            int bit=i+2;
            int beg=beginer[i];
            cout<<beg<<endl;
            int add=addtot[i];
            for(int j=0;j<(10-bit);++j){
                if((beg>=low) && (beg<=high)) {
                    cout<<beg<<endl;
                    res.push_back(beg);}
                beg+=add;
            }
        }
        return res;
    }
};

 

posted @ 2022-01-23 23:14  aalanwyr  阅读(41)  评论(0编辑  收藏  举报