-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathCountAndSay.java
More file actions
39 lines (31 loc) · 964 Bytes
/
CountAndSay.java
File metadata and controls
39 lines (31 loc) · 964 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
38
39
public class Solution {
public String countAndSay(int n) {
// Start typing your Java solution below
// DO NOT write main() function
if(n==1) return "1";
String re = "1";
int c = 1;
while(c<n){
String temp = "";
char current = re.charAt(0);
int count = 0;
for(int i=0 ;i<re.length();){
if(re.charAt(i) == current){
count ++;
i++;
}
else{
temp += Integer.toString(count);
temp += current;
current = re.charAt(i);
count = 0;
}
}
temp += Integer.toString(count);
temp += current;
re = temp;
c++;
}
return re;
}
}