forked from chuyi2007/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCountAndSay.java
More file actions
27 lines (26 loc) · 832 Bytes
/
CountAndSay.java
File metadata and controls
27 lines (26 loc) · 832 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
//O(N^2)
public class Solution {
public String countAndSay(int n) {
// Start typing your Java solution below
// DO NOT write main() function
String result = "1";
for(int i = 1; i < n; ++i){
String tmp = "";
int curCount = 1;
for(int j = 0; j < result.length(); ++j){
char curChar = result.charAt(j);
if(j == result.length() - 1
|| curChar != result.charAt(j + 1)){
tmp += String.valueOf(curCount)
+ Character.toString(curChar);
curCount = 1;
}
else{
curCount++;
}
}
result = tmp;
}
return result;
}
}