clear code with javascript implemented


  • 0
    A
    /**
     * @param {number} n
     * @return {string}
     */
    var countAndSay = function(n) {
        if(!n){
            return '';
        }
        if(n===1){
            return '1';
        }
        var pre=[1],i=2,cur=[];
        while(i<=n){
            var j=0,count=0,num=0;
            while(j<pre.length){
                if(num!==pre[j] && count>0 && num>0){
                    cur.push(count);
                    cur.push(num);
                    count=0;
                    num=0;
                }
                num = pre[j++];
                count++;
            }
            if(count>0 && num>0){
                cur.push(count);
                cur.push(num);
            }
            i++;
            pre=cur;
            cur=[];
        }
        return pre.join('');
    };
    

Log in to reply
 

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