【LeetCode题解】386. Lexicographical Numbers

本题意味将1到n所有的int类型数进行字典序排序

一、递归思想

首先想到的是运用分治算法的思想,设采用solve的方法解决问题

对于整数m,若在某一时刻将其插入最终返回的res数组,思考下一步应该插入什么,分两种情况:

(1)若 m*10 <= n,则将 m*10 插入res数组,并继续思考下一步插入什么

(2)若 m < n 且 m%10 <  9 ,则将 m+1 插入res数组,并继续思考下一步插入什么

此处取模10小于9的情况,是因为当余数为9时,+1后得到的情况已经在条件(1)中考虑到了,加入会产生重复

下面给出解题代码:

class Solution {
public:
    vector<int> lexicalOrder(int n) {
        this->n = n;
        solve(1);
        return res;
        
    }
private:
    int n;
    vector<int> res;
    void solve(int m){
        res.push_back(m);
        if(m * 10 <= n){
            solve(m * 10);
        }
        if(m < n && m % 10 < 9){
            solve(m + 1);
        }
    }
};

二、非递归思想(栈)

如果不用递归的方法,也可以使用堆栈的思想进行编程,大致过程可以借鉴上文中说到的思想
需要注意的是,因为后进先出的思想,所以需要先将字典序较小的压入栈,再将字典序较大的压入栈,两个if语句块不能颠倒顺序
下面给出具体的代码:
class Solution {
public:
    vector<int> lexicalOrder(int n) {
        vector<int> res;
        stack<int> s;
        s.push(1);
        while(!s.empty()){
            int tmp = s.top();
            s.pop();
            res.push_back(tmp);
            if(tmp < n && tmp % 10 < 9){
                s.push(tmp + 1);
            }
            if(tmp * 10 <= n){
                s.push(tmp * 10);
            }
        }
        return res;
    }
};
此外附上另一种非递归写法,类似DFS的思想:
class Solution {
public:
    vector<int> lexicalOrder(int n) {
        vector<int> res;
        stack<int> s;
        int x = 1;
        while(x <= n){
            s.push(x);
            res.push_back(x);
            x *= 10;
        }
        while(!s.empty()){
            int tmp = s.top();
            s.pop();
            if(tmp % 10 == 9)
                continue;
            tmp += 1;
            while(tmp <= n){
                s.push(tmp);
                res.push_back(tmp);
                tmp *= 10;
            }
        }
        return res;
    }
};



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值