2.Count and Say
Last updated
Was this helpful?
Last updated
Was this helpful?
看幾層就跑幾次, 每次比較同一層間前後的字元是否相同, 並儲存counter.
C solution
JavaScript solution
res="" oldAry = []; oldAry.push(1) ary = []; ary.push(1) round = 0; count = 1; index = 0; len = 1; while(round < n-1){ round++; index = 0 for(i = 0 ; i < len; i++){ if (oldAry[i] === oldAry[i+1] ) { count++ }else{ ary[index++] = count; ary[index++] = oldAry[i]; count = 1; } } len = index ; oldAry=ary.slice(0) } ary.forEach(function(element, index, array){ res+=element }) return res };