Javascript easily understand solution


  • 0
    S

    spend sometimes to parse the string and the next thing is easy

    /**
     * @param {string} s
     * @return {string}
     */
    var decodeString = function(s) {
        
        var parsedS = JSON.parse('[' + [].slice.call(s).map(_ => {
          if(['[',']'].indexOf(_) > -1) {
            return _;
          } else {
            return ',"' + _ + '"'
          }
        }).join('').replace(/\[\,/g, ',[').slice(1) + ']');
        
        return helper(1, parsedS);
    };
    
    function helper (times, current) {
        var result = '';
        var clength = current.length;
        var numStack = []
        for(var i = 0 ; i < clength ; i ++) {
            if(Number(current[i]) == current[i]) {
                numStack.push(current[i]);
            } else if (current[i] instanceof Array) {
                result += helper(Number(numStack.join('')), current[i]);
                numStack = [];
            } else {
                result += current[i];
            }
        }
        
        if(times === 1) {
            return result;
        }
        
        var ret = '';
        while(times) {
            ret += result;
            times --;
        }
        
        return ret;
    }
    

Log in to reply
 

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