Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
31 lines (29 loc) · 793 Bytes

File metadata and controls

31 lines (29 loc) · 793 Bytes
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
package leetcode._38_;
class Solution {
public String countAndSay(int n) {
String tmp = "1";
if (n == 1) {
return tmp;
}
for (int i = 1; i < n; i++) {
tmp = say(tmp);
}
return tmp;
}
private String say(String numStr) {
char tmp = numStr.charAt(0);
int counter = 1;
StringBuilder sb = new StringBuilder();
for (int i = 1; i < numStr.length(); i++) {
if (numStr.charAt(i) == numStr.charAt(i - 1)) {
counter++;
} else {
sb.append(counter).append(tmp);
tmp = numStr.charAt(i);
counter = 1;
}
}
sb.append(counter).append(tmp);
return sb.toString();
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.