JS solution, do not know why not pass


  • 0
    T

    Re: 402. Remove K Digits -- javascript code

    /**
     * @param {string} num
     * @param {number} k
     * @return {string}
     */
    var removeKdigits = function(num, k) {
        
        return removeRec(num,k)
    };
    
    var removeRec = function(num,k) {
    
        if (num == '0') return '0' 
        if (k===0) return num.replace(/\b0+/g, '')
        
        num = num.replace(/\b0+/g, '')
        
    
        
        if (num.length <=k ) return '0'
    
        if(num.length == 2) {
            
            if (parseInt(num[0])>=parseInt(num[1])){
                num = num[1]
            }
            else {
                num = num[0] + num.slice(2)
            }
            return removeRec(num,k-1)
    
        }
    
        
        if (parseInt(num[0])>=parseInt(num[1])){
            num = num.slice(1)
        }else if (parseInt(num[1])>=parseInt(num[2])) {
            num = num[0] + num.slice(2)
        }else {
            num = num[0] + num[1] + num.slice(3)
        }
        
        return removeRec(num,k-1)
        
    };```

Log in to reply
 

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