Easy
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.
10ms:
public String countAndSay(int n) {
if(n==1) return "1";
String[] ca = new String[n];
ca[0] = "1";
for(int i=1;i<n;i++){
String pre = ca[i-1];
StringBuilder now = new StringBuilder();;
int count = 0;
for(int j=0;j<pre.length();){
while(count+j<pre.length()&&pre.charAt(j)==pre.charAt(count+j))
count++;
now.append(count).append(pre.charAt(j));
j += count;
count = 0;
}
ca[i] = now.toString();
}
return ca[n-1];
}