C++ solution using backtracking


  • 0
    S
    void backtracking(vector<int>&res,int&num,int n){
            if(num<=n){
                res.push_back(num);
                for(int i=0;i<=9;i++){
                    int tmp = num*10+i;
                    backtracking(res,tmp,n);
                    tmp = num/10;
                }
            }
        }
        vector<int> lexicalOrder(int n) {
            vector<int> res;
            if(n==0) return res;
            for(int num=1;num<=9;num++) backtracking(res,num,n);
            return res;
        }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.