-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCount_and_Say.cpp
More file actions
32 lines (30 loc) · 923 Bytes
/
Count_and_Say.cpp
File metadata and controls
32 lines (30 loc) · 923 Bytes
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
/*
O(N*N)
*/
class Solution {
public:
string countAndSay(int n) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
string result = "1";
for (int i = 1; i < n; i++) {
int k = 0, count = 0;
stringstream os;
for (int j = 0; j < result.size(); j++) {
if (result[j] == result[k]) {
count++;
} else {
os << count << result[k];
count = 0;
k = j;
j--;
}
}
if (count > 0)
os << count << result[k];
result = os.str();
//cout << result << endl;
}
return result;
}
};