Skip to content

Commit 996a283

Browse files
committed
remove 38 Count and Say
1 parent dcd09db commit 996a283

File tree

3 files changed

+52
-12
lines changed

3 files changed

+52
-12
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
* 014. [Longest Common Prefix](String/14_LongestCommonPrefix.py)
2424
* 017. [Letter Combinations of a Phone Number](String/17_LetterCombinationsPN.py)
2525
* 028. [Implement strStr()](String/28_ImplementstrStr.py)
26+
* 038. [Count and Say](String/38_CountAndSay.py)
2627
* 058. [Length of Last Word](String/58_LengthOfLastWord.py)
2728
* 067. [Add Binary](String/67_AddBinary.py)
2829
* 068. Text Justification

String/38_CountAndSay.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* @Author: [email protected]
3+
* @Last Modified time: 2016-07-14 19:12:37
4+
*/
5+
6+
class Solution {
7+
public:
8+
string countAndSay(int n) {
9+
int index = 2;
10+
string pre_str = "1";
11+
while(index <= n){
12+
string new_str = "";
13+
int i=0;
14+
while(i < pre_str.size()){
15+
int count = 1;
16+
char c = pre_str[i];
17+
i += 1;
18+
while(i < pre_str.size() && pre_str[i]==c){
19+
count++;
20+
i++;
21+
}
22+
new_str += to_string(count) + c;
23+
}
24+
pre_str = new_str;
25+
index ++;
26+
}
27+
return pre_str;
28+
}
29+
};
30+
31+
/*
32+
1
33+
5
34+
15
35+
*/

Week05/38.py renamed to String/38_CountAndSay.py

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33

44

55
class Solution(object):
6+
""" Quite straight-forward solution.
7+
8+
We generate k-th string, and from k-th string we generate k+1-th string,
9+
until we generate n-th string.
10+
"""
611
def countAndSay(self, n):
7-
"""
8-
:type n: int
9-
:rtype: str
10-
"""
1112
if n <= 1:
1213
return "1"
1314

@@ -16,23 +17,26 @@ def countAndSay(self, n):
1617
# Get the ith count-and-say sequence by scan pre_str
1718
length = len(pre_str)
1819
current_str = ""
19-
index = 0
2020

2121
# Count and say the pre_str
22+
index = 0
2223
while index < length:
2324
char = pre_str[index]
2425
repeat = 0
25-
pos = repeat + index + 1
26+
pos = index + 1
2627
while pos < length and pre_str[pos] == char:
2728
repeat += 1
28-
pos = repeat + index + 1
29+
pos += 1
2930

30-
if repeat:
31-
current_str = current_str + str(repeat + 1) + char
32-
else:
33-
current_str = current_str + "1" + char
34-
index = index + repeat + 1
31+
current_str += str(repeat + 1) + char
32+
index = pos
3533

3634
pre_str = current_str
3735

3836
return pre_str
37+
38+
"""
39+
1
40+
5
41+
15
42+
"""

0 commit comments

Comments
 (0)