Hi, this is Shunchi!

  • Home

  • Tags0

  • Archives267

  • Categories0

  • Curricula

  • DSA

  • LeetCode_Notes

  • Interviews

  • General

  • Resume

38. Count and Say

Posted on 2020-06-28 | Edited on 2021-01-22

LeetCode

Recursion link

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Solution {
public String countAndSay(int n) {
if(n == 1) return "1";
String prev = countAndSay(n - 1);
StringBuilder str = new StringBuilder();
int i = 0;
while(i < prev.length()) {
char curr = prev.charAt(i);
int j = 0;
while(i+j < prev.length() && prev.charAt(i+j) == curr) j++;
str.append(j);
str.append(curr);
i += j;
}
return str.toString();
}
}

<1…130131132…267>
ShunchiZhou

ShunchiZhou

267 posts
RSS
GitHub E-Mail Gitbook Linkedin
© 2024 ShunchiZhou
Powered by Hexo v5.4.0
|
0%