Skip to content

Commit 197d77c

Browse files
committed
Time: 8 ms (56.33%), Space: 41.7 MB (63.58%) - LeetHub
1 parent e4f3833 commit 197d77c

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class Solution {
2+
fun countAndSay(n: Int): String {
3+
var result = "1"
4+
5+
repeat(n - 1) {
6+
val newResult = StringBuilder()
7+
8+
var count = 1
9+
var prev = result[0].digitToInt()
10+
11+
for (i in 1 until result.length) {
12+
val num = result[i].digitToInt()
13+
if (num == prev) {
14+
++count
15+
}
16+
else {
17+
newResult.append(count)
18+
newResult.append(prev)
19+
20+
prev = num
21+
count = 1
22+
}
23+
}
24+
newResult.append(count)
25+
newResult.append(prev)
26+
27+
result = newResult.toString()
28+
}
29+
30+
return result
31+
}
32+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy