The fastest js solution. 116ms


  • 2
    K
    var countAndSay = function(n) {
      var start = '1';
      var result = '';
      var tmp = 1;
      for (i=0; i<n-1; i++){
        //tmp will count all repeats
        for (var j=0; j<start.length; j++){
          if(start[j] !== start[j+1] || j == start.length - 1){
            result += tmp + start[j];
            tmp = 1;
          } else {
            tmp +=1;
          }
        }
        start = result;
        result = '';
      }
      return start;
    };

Log in to reply
 

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