-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCountandSay.java
More file actions
35 lines (35 loc) · 981 Bytes
/
CountandSay.java
File metadata and controls
35 lines (35 loc) · 981 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
package easy;
/**
*
* ClassName: CountandSay
* @author chenyiAlone
* Create Time: 2019/03/26 17:01:31
* Description: No.38
* 思路:
* 1. 从 n == 1 开始,last 记录每次报数的结果
* 2. 更新 count 统计的 last 相同连续字符数量以及当前字符到 tmp
* 3. 将 last 更新为 tmp
*
*/
public class CountandSay {
public String countAndSay(int n) {
String last = "1";
for (int i = 0; i < n; i++) {
String tmp = "";
int j = 0;
while (j < last.length()) {
int count = 1;
char c = last.charAt(j);
while (j + 1 < last.length() && last.charAt(j + 1) == c) {
count++;
j++;
}
tmp += count;
tmp += String.valueOf(c);
j++;
}
last = tmp;
}
return last;
}
}