Skip to content

Commit aec5d9f

Browse files
committed
Add solution for leetcode 38.
1 parent 4c949c4 commit aec5d9f

File tree

3 files changed

+147
-0
lines changed

3 files changed

+147
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#include <string>
2+
#include <iostream>
3+
// #include <algorithm>
4+
using namespace std;
5+
6+
/*
7+
主体思路: 以已知a4=1211, 来求a5为例。
8+
首先将不同字符间(用虚线)划开进行分片,即 1|2|11,分别统计各个分片中连续相同的字符数即可。
9+
*/
10+
class Solution {
11+
public:
12+
string countAndSay(int n) {
13+
if(n == 1)
14+
return "1"; // f(1) = 1
15+
16+
string res = "1"; // f(1) = 1, 作为迭代的初始值放入到结果中
17+
for(int i=0; i<n-1; i++)
18+
{
19+
string currentCombinedStr = "";
20+
char curFirstChar = res[0]; // 存放当前分片的第一个字符
21+
int currentCharCount = 0;
22+
for(char ch : res) // 将当前的字符与当前分片的第一个字符比较
23+
{
24+
if(ch == curFirstChar)
25+
currentCharCount += 1;
26+
else {
27+
// 出现新的字符时,把已处理的连续相同字符的信息插入到结果字符串中
28+
currentCombinedStr.append(to_string(currentCharCount));
29+
currentCombinedStr.push_back(curFirstChar);
30+
31+
curFirstChar = ch;
32+
currentCharCount = 1;
33+
}
34+
}
35+
36+
// 把末尾连续相同字符的信息插入到结果字符串中(对末尾一段字符来说,不会再有新的字符了)
37+
currentCombinedStr.append(to_string(currentCharCount));
38+
currentCombinedStr.push_back(curFirstChar);
39+
res = currentCombinedStr; // 将结果用作下一轮循环的初始值
40+
}
41+
42+
return res;
43+
}
44+
};
45+
46+
// Test
47+
int main()
48+
{
49+
Solution sol;
50+
int n = 9;
51+
string res = sol.countAndSay(n);
52+
cout << res << endl;
53+
54+
return 0;
55+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#include <string>
2+
#include <iostream>
3+
// #include <algorithm>
4+
using namespace std;
5+
6+
/*
7+
主体思路: 以已知a4=1211, 来求a5为例。
8+
首先将不同字符间(用虚线)划开进行分片,即 1|2|11,分别统计各个分片中连续相同的字符数即可。
9+
*/
10+
class Solution {
11+
public:
12+
string countAndSay(int n) {
13+
string res = "1"; // f(1) = 1
14+
15+
while (n > 1) {
16+
string curStr = "";
17+
for (int i = 0; i < res.size(); i++) {
18+
int count = getRepeatCount(res.substr(i)); // 截取从当前字符到末尾的子串
19+
curStr += to_string(count);
20+
curStr.push_back(res[i]);
21+
22+
// 跳过重复的字符, 共处理一次即可
23+
i = i + count - 1;
24+
}
25+
n--; // 总共需要迭代 n-1 次
26+
27+
res = curStr; // 将结果用作下一轮循环的初始值
28+
}
29+
return res;
30+
}
31+
32+
/* 得到字符串 str 中第一个分片中连续相等数的重复个数,例如: "111221" 返回 3, "2" 返回 1 */
33+
int getRepeatCount(string str) {
34+
int count = 1;
35+
char same = str[0];
36+
for (int i = 1; i < str.size(); i++) {
37+
if (same == str[i]) {
38+
count++;
39+
} else {
40+
break;
41+
}
42+
}
43+
return count;
44+
}
45+
};
46+
47+
// Test
48+
int main()
49+
{
50+
Solution sol;
51+
int n = 9;
52+
string res = sol.countAndSay(n);
53+
cout << res << endl;
54+
55+
return 0;
56+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#include <string>
2+
#include <iostream>
3+
// #include <algorithm>
4+
using namespace std;
5+
6+
class Solution {
7+
public:
8+
string countAndSay(int n) {
9+
if (n <= 0) return "";
10+
string res = "1";
11+
while (--n) {
12+
string curStr = "";
13+
for (int i = 0; i < res.size(); ++i) {
14+
int count = 1; // 出现第一个新字符, count置为1
15+
while (i + 1 < res.size() && res[i] == res[i + 1]) { /* 这里与上1层循环用的是同一个i, 且区间是上层循环的子区间, 故时间复杂度是O(n^2) */
16+
count++;
17+
i++;
18+
}
19+
curStr += to_string(count) + res[i];
20+
}
21+
res = curStr; // 将结果用作下一轮循环的初始值
22+
}
23+
return res;
24+
}
25+
};
26+
27+
// Test
28+
int main()
29+
{
30+
Solution sol;
31+
int n = 9;
32+
string res = sol.countAndSay(n);
33+
cout << res << endl;
34+
35+
return 0;
36+
}

0 commit comments

Comments
 (0)