-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCountandSay.java
More file actions
37 lines (33 loc) · 915 Bytes
/
Copy pathCountandSay.java
File metadata and controls
37 lines (33 loc) · 915 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/**
* @author li_zhe
* 该题题意不清楚,题意:当前为把上一个数字读一遍的结果,相同数字合并一起
*/
public class CountandSay {
public String countAndSay(int n) {
StringBuilder sb1 = new StringBuilder("1");
StringBuilder sb2 = new StringBuilder();
n--;
while(n-- > 0){
int count = 1;
for(int i = 0; i < sb1.length(); i++){
if(i + 1 < sb1.length() && sb1.charAt(i) == sb1.charAt(i + 1)){
count++; continue;
}
sb2.append(String.valueOf(count));
sb2.append(sb1.charAt(i));
count = 1;
}
sb1 = sb2;
sb2 = new StringBuilder();
}
return sb1.toString();
}
public static void main(String[] args) {
CountandSay count = new CountandSay();
System.out.println(count.countAndSay(1));
System.out.println(count.countAndSay(2));
System.out.println(count.countAndSay(3));
System.out.println(count.countAndSay(5));
System.out.println(count.countAndSay(10));
}
}