報(bào)數(shù)
public class Solution {
/**
* @param n: the nth
* @return: the nth sequence
*/
public String countAndSay(int n) {
// write your code here
String start = "1";
int index = 1;
while(index < n){
StringBuilder temp = new StringBuilder();
for(int i = 0; i < start.length(); i++){
char c = start.charAt(i);
if(i + 1 < start.length()){
if(c == start.charAt(i + 1)){
int num = 1;
int k = i;
for(int j = k + 1; j < start.length(); j++){
if(start.charAt(j) == c){
num++;
i++;
}else{
break;
}
}
temp.append(num + ""+ c);
}else{
temp.append("1" + c);
}
}else{
temp.append("1" + c);
}
}
start = temp.toString();
index++;
}
return start;
}
}
報(bào)數(shù)指的是窗价,按照其中的整數(shù)的順序進(jìn)行報(bào)數(shù),然后得到下一個(gè)數(shù)屈糊。如下所示:
1, 11, 21, 1211, 111221, ...
1 讀作 "one 1" -> 11.
11 讀作 "two 1s" -> 21.
21 讀作 "one 2, then one 1" -> 1211.
給定一個(gè)整數(shù) n, 返回 第 n 個(gè)順序钱烟。
解題思路
通過(guò)循環(huán)遍歷找相同的數(shù)