-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcountAndSay.java
More file actions
35 lines (33 loc) · 1014 Bytes
/
countAndSay.java
File metadata and controls
35 lines (33 loc) · 1014 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
/**
* Author : WindAsMe
* File : countAndSay.java
* Time : Create on 18-8-31
* Location : ../Home/JavaForLeeCode2/countAndSay.java
* Function : LeetCode No.38
*/
public class countAndSay {
private static String countAndSayResult(int n) {
StringBuilder res = new StringBuilder("1");
StringBuilder prev;
for (int i = 1; i < n; i++) {
prev = res;
res = new StringBuilder();
char cur = prev.charAt(0);
int count = 1;
for (int j = 1; j < prev.length(); j++) {
if (cur == prev.charAt(j))
count++;
else{
res.append(count).append(cur);
cur = prev.charAt(j);
count = 1;
}
}
res.append(count).append(cur);
}
return res.toString();
}
public static void main(String[] args) {
System.out.println(countAndSayResult(2));
}
}