class Solution {
public String countAndSay(int n) {
if (n == 1) return "1";
StringBuilder sb = new StringBuilder();
String s = countAndSay(n - 1);
char value = s.charAt(0);
int cnt = 0;
for (int i = 0; i < s.length(); i++) {
char target = s.charAt(i);
if (target == value) {
cnt++;
continue;
}
sb.append(cnt).append(value);
value = target;
cnt = 1;
}
sb.append(cnt).append(value);
return sb.toString();
}
}
'문제풀이' 카테고리의 다른 글
프로그래머스 K번째 수 Java (0) | 2021.03.24 |
---|---|
Leetcode Coin Change Java (0) | 2021.01.20 |
Leetcode MinStack JAVA (0) | 2021.01.05 |
Leetcode Climbing Stairs JAVA (0) | 2021.01.04 |
Leetcode Happy Number JAVA (0) | 2021.01.04 |