JavaScript simple recursive solution


  • 0
    D
    
    /**
     * @param {number} n
     * @return {number[]}
     */
    var lexicalOrder = function(n) {
        
        if(n < 1){
            return [];
        }
        
        let hash = {};
        let arr = [];
        
        for(let i = 1; i < 10; i++){
            if(i <= n){
                arr.push(i);
                util(i);
            }
        }
        
        return arr;
        
        function util(p){
            for(let i = 0; i < 10; i++){
                let curr = p * 10 + i;
    
                if(curr > n){
                    return;
                }
                else {
                    arr.push(curr);
                }
                
                util(curr);
            }
        }
        
    };
    
    

Log in to reply
 

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