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.
一開(kāi)始不想做這道題,覺(jué)得沒(méi)啥意思丑孩,后來(lái)還是做了冀宴,確實(shí)沒(méi)啥意思。温学。略贮。
var countAndSay = function(n) {
var now = "1";
var result = "";
while (n!==1) {
var p = now[0];
var count = 0;
for (var i = 0; i<now.length;i++) {
if (now[i]===p) {
count++;
} else {
result+=count;
result+=p;
p=now[i];
count = 1;
}
}
result+=count;
result+=p;
now = result;
result = "";
n--;
}
return now;
};