Sunday, September 4, 2016

Solution Leetcode 386. Lexicographical Numbers


Following is the link to description of the problem

https://leetcode.com/problems/lexicographical-numbers/


Solution code

class Solution {
    vector<int> v;
    int limit;
private:
    void generateNos(int nos){
        if (nos > limit){
            return;
        }
       
        v.push_back(nos);
       
        for (int i=0;i<10;i++){
       
            int temp = (nos * 10) + i;
            generateNos(temp);
           
        }
        return ;
    }
public:

    vector<int> lexicalOrder(int n) {
        limit = n;
        for (int i=1;i<10;i++){
            generateNos(i);
        }
        return v;
    }
};

Time taken : 1259 msec
Status: Accepted

No comments:

Post a Comment