-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleetcode_38.cpp
More file actions
64 lines (56 loc) · 1.61 KB
/
Copy pathleetcode_38.cpp
File metadata and controls
64 lines (56 loc) · 1.61 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// my solution
class Solution {
public:
string countAndSay(int n) {
if(n == 1) return "1";
string res = "1";
for(int i = 1; i < n; i++) {
res = RLE(res);
}
return res;
}
private:
string RLE(string &s) {
string res;
int cnt = 1;
for (int i = 0; i < s.size(); i++) {
if(i == s.size() - 1 || s[i] != s[i + 1]) {
res += to_string(cnt) + s[i];
cnt = 1;
}
else {
cnt ++;
}
}
return res;
}
};
// optimal solution
class Solution {
public:
string countAndSay(int n) {
string result = "1";
for (int i = 1; i < n; ++i) {
string temp;
// 현재 result의 길이를 고려하여 최대 2배 길이로 예약
temp.reserve(result.size() * 2);
int len = result.size();
int j = 0;
while (j < len) {
char currentChar = result[j];
int count = 0;
// 같은 숫자가 연속되는 개수 세기
while (j < len && result[j] == currentChar) {
++count;
++j;
}
// count가 한 자릿수임을 가정하여 문자로 변환
temp.push_back('0' + count);
temp.push_back(currentChar);
}
// 새로운 문자열이 result가 됨 (move로 불필요한 복사 줄이기)
result = move(temp);
}
return result;
}
};