Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
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)