-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution38.java
More file actions
35 lines (33 loc) · 986 Bytes
/
Solution38.java
File metadata and controls
35 lines (33 loc) · 986 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
/**
* Created by Alex on 2017/3/17.
*/
public class Solution38 {
public String countAndSay(int n) {
if(n == 0){
return null;
}else {
n--;
}
String number = "1";
for(int i = 0; i < n; i++){
String currentNumber = number.substring(0, 1);
int times = 0;
StringBuilder result = new StringBuilder();
for(int j = 0; j < number.length(); j++){
String tempNumber = number.substring(j, j+1);
if(tempNumber.equals(currentNumber)){
times++;
}else {
result.append(times);
result.append(currentNumber);
currentNumber = tempNumber;
times = 1;
}
}
result.append(times);
result.append(currentNumber);
number = result.toString();
}
return number;
}
}