LeetCode 38. Count and Say

題目

The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, 1211, 111221, ...

1 is read off as "one 1" or 11. 11 is read off as "two 1s" or 21. 21 is read off as "one 2, then one 1" or 1211. Given an integer n, generate the nth sequence.

Note: The sequence of integers will be represented as a string.

翻譯

這是一個算完說出來的序列,序列如下:
1, 11, 21, 1211, 111221, ...

1     讀做 1個1, 所以下一個變成 11  
11    讀做 2個1, 也就是21  
21    讀做 1個2 1個1,得到1211
1211  1個1,1個2,2個1  111221  

思路

相信這題很多人跟我一樣,看懂題目後就覺得簡單了,直接看下面的code就行。

解題

/**
 * @param {number} n
 * @return {string}
 */
var countAndSay = function(n) {
    if(n <= 1) return "1";

    var countSay = '1';

    for(var i = 2 ; i <= n ; i++){
        var num = countSay.charAt(0); //從開頭開始數
        var temp = countSay;
        var count = 1;

        countSay = ''; // 清空儲存這輪數完的字串


        for(var j = 1 ; j < temp.length; j++){
            // 數字相同,count++
            if(temp.charAt(j) == num){
                count++;
            } else {
                // 數字不同,將目前的count與num加到字串,更新num,新的num從1開始數起
                countSay += count;     
                countSay += num;
                num = temp.charAt(j);
                count = 1;
            }
        }
        countSay += count;     
        countSay += num;
    }
    return countSay;

};

results matching ""

    No results matching ""