Skip to content

Commit 4857958

Browse files
committed
feat: update solutions to lc problem: No.0038
No.0038.Count and Say
1 parent 48c2372 commit 4857958

File tree

6 files changed

+223
-56
lines changed

6 files changed

+223
-56
lines changed

solution/0000-0099/0038.Count and Say/README.md

+81-2
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ countAndSay(4) = 读 "21" = 一 个 2 + 一 个 1 = "12" + "11" = "1211"
6969
<li><code>1 <= n <= 30</code></li>
7070
</ul>
7171

72-
7372
## 解法
7473

7574
<!-- 这里可写通用的实现逻辑 -->
@@ -81,15 +80,95 @@ countAndSay(4) = 读 "21" = 一 个 2 + 一 个 1 = "12" + "11" = "1211"
8180
<!-- 这里可写当前语言的特殊实现逻辑 -->
8281

8382
```python
84-
83+
class Solution:
84+
def countAndSay(self, n: int) -> str:
85+
s = '1'
86+
for _ in range(n - 1):
87+
i = 0
88+
t = []
89+
while i < len(s):
90+
j = i
91+
while j < len(s) and s[j] == s[i]:
92+
j += 1
93+
t.append(str(j - i))
94+
t.append(str(s[i]))
95+
i = j
96+
s = ''.join(t)
97+
return s
8598
```
8699

87100
### **Java**
88101

89102
<!-- 这里可写当前语言的特殊实现逻辑 -->
90103

91104
```java
105+
class Solution {
106+
public String countAndSay(int n) {
107+
String s = "1";
108+
while (--n > 0) {
109+
StringBuilder t = new StringBuilder();
110+
for (int i = 0; i < s.length();) {
111+
int j = i;
112+
while (j < s.length() && s.charAt(j) == s.charAt(i)) {
113+
++j;
114+
}
115+
t.append((j - i) + "");
116+
t.append(s.charAt(i));
117+
i = j;
118+
}
119+
s = t.toString();
120+
}
121+
return s;
122+
}
123+
}
124+
```
125+
126+
### **C++**
127+
128+
```cpp
129+
class Solution {
130+
public:
131+
string countAndSay(int n) {
132+
string s = "1";
133+
while (--n)
134+
{
135+
string t = "";
136+
for (int i = 0; i < s.size();)
137+
{
138+
int j = i;
139+
while (j < s.size() && s[j] == s[i]) ++j;
140+
t += to_string(j - i);
141+
t += s[i];
142+
i = j;
143+
}
144+
s = t;
145+
}
146+
return s;
147+
}
148+
};
149+
```
92150
151+
### **Go**
152+
153+
```go
154+
func countAndSay(n int) string {
155+
s := "1"
156+
for k := 0; k < n-1; k++ {
157+
t := &strings.Builder{}
158+
i := 0
159+
for i < len(s) {
160+
j := i
161+
for j < len(s) && s[j] == s[i] {
162+
j++
163+
}
164+
t.WriteString(strconv.Itoa(j - i))
165+
t.WriteByte(s[i])
166+
i = j
167+
}
168+
s = t.String()
169+
}
170+
return s
171+
}
93172
```
94173

95174
### **...**

solution/0000-0099/0038.Count and Say/README_EN.md

+81-2
Original file line numberDiff line numberDiff line change
@@ -45,21 +45,100 @@ countAndSay(4) = say &quot;21&quot; = one 2 + one 1 = &quot;12&quot; + &quot;11&
4545
<li><code>1 &lt;= n &lt;= 30</code></li>
4646
</ul>
4747

48-
4948
## Solutions
5049

5150
<!-- tabs:start -->
5251

5352
### **Python3**
5453

5554
```python
56-
55+
class Solution:
56+
def countAndSay(self, n: int) -> str:
57+
s = '1'
58+
for _ in range(n - 1):
59+
i = 0
60+
t = []
61+
while i < len(s):
62+
j = i
63+
while j < len(s) and s[j] == s[i]:
64+
j += 1
65+
t.append(str(j - i))
66+
t.append(str(s[i]))
67+
i = j
68+
s = ''.join(t)
69+
return s
5770
```
5871

5972
### **Java**
6073

6174
```java
75+
class Solution {
76+
public String countAndSay(int n) {
77+
String s = "1";
78+
while (--n > 0) {
79+
StringBuilder t = new StringBuilder();
80+
for (int i = 0; i < s.length();) {
81+
int j = i;
82+
while (j < s.length() && s.charAt(j) == s.charAt(i)) {
83+
++j;
84+
}
85+
t.append((j - i) + "");
86+
t.append(s.charAt(i));
87+
i = j;
88+
}
89+
s = t.toString();
90+
}
91+
return s;
92+
}
93+
}
94+
```
95+
96+
### **C++**
97+
98+
```cpp
99+
class Solution {
100+
public:
101+
string countAndSay(int n) {
102+
string s = "1";
103+
while (--n)
104+
{
105+
string t = "";
106+
for (int i = 0; i < s.size();)
107+
{
108+
int j = i;
109+
while (j < s.size() && s[j] == s[i]) ++j;
110+
t += to_string(j - i);
111+
t += s[i];
112+
i = j;
113+
}
114+
s = t;
115+
}
116+
return s;
117+
}
118+
};
119+
```
62120
121+
### **Go**
122+
123+
```go
124+
func countAndSay(n int) string {
125+
s := "1"
126+
for k := 0; k < n-1; k++ {
127+
t := &strings.Builder{}
128+
i := 0
129+
for i < len(s) {
130+
j := i
131+
for j < len(s) && s[j] == s[i] {
132+
j++
133+
}
134+
t.WriteString(strconv.Itoa(j - i))
135+
t.WriteByte(s[i])
136+
i = j
137+
}
138+
s = t.String()
139+
}
140+
return s
141+
}
63142
```
64143

65144
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
public:
3+
string countAndSay(int n) {
4+
string s = "1";
5+
while (--n)
6+
{
7+
string t = "";
8+
for (int i = 0; i < s.size();)
9+
{
10+
int j = i;
11+
while (j < s.size() && s[j] == s[i]) ++j;
12+
t += to_string(j - i);
13+
t += s[i];
14+
i = j;
15+
}
16+
s = t;
17+
}
18+
return s;
19+
}
20+
};
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,18 @@
11
func countAndSay(n int) string {
2-
buf := bytes.NewBufferString("1")
3-
for i := 2; i <= n; i++ {
4-
s := buf.String()
5-
c, l := s[0:1], len(s)
6-
buf.Reset()
7-
count := 0
8-
for j := 0; j < l; j++ {
9-
if c == s[j:j+1] {
10-
count++
11-
} else {
12-
buf.WriteByte(byte(48 + count))
13-
buf.WriteString(c)
14-
count = 1
15-
c = s[j : j+1]
2+
s := "1"
3+
for k := 0; k < n-1; k++ {
4+
t := &strings.Builder{}
5+
i := 0
6+
for i < len(s) {
7+
j := i
8+
for j < len(s) && s[j] == s[i] {
9+
j++
1610
}
11+
t.WriteString(strconv.Itoa(j - i))
12+
t.WriteByte(s[i])
13+
i = j
1714
}
18-
buf.WriteByte(byte(48 + count))
19-
buf.WriteString(c)
15+
s = t.String()
2016
}
21-
return buf.String()
22-
}
17+
return s
18+
}
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,19 @@
11
class Solution {
22
public String countAndSay(int n) {
3-
String one = "1";
4-
while (n > 1) {
5-
one = say(one);
6-
n--;
7-
}
8-
return one;
9-
}
10-
11-
private String say(String las) {
12-
StringBuilder sBuilder = new StringBuilder();
13-
int l = 1;
14-
for (int i = 0; i < las.length(); i++) {
15-
if (i < las.length() - 1 && las.charAt(i) == las.charAt(i + 1)) l++;
16-
else {
17-
sBuilder.append(l).append(las.charAt(i));
18-
l = 1;
3+
String s = "1";
4+
while (--n > 0) {
5+
StringBuilder t = new StringBuilder();
6+
for (int i = 0; i < s.length();) {
7+
int j = i;
8+
while (j < s.length() && s.charAt(j) == s.charAt(i)) {
9+
++j;
10+
}
11+
t.append((j - i) + "");
12+
t.append(s.charAt(i));
13+
i = j;
1914
}
15+
s = t.toString();
2016
}
21-
return sBuilder.toString();
17+
return s;
2218
}
2319
}
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,15 @@
1-
class Solution(object):
2-
def countAndSay(self, n):
3-
s = '1' #初始化第一个数字
4-
for i in range(n-1): #n-1个数
5-
temp = '' #一个空数列
6-
num = s[0] #s的第一位数字
7-
count = 0 #当前数字的个数
8-
9-
for j in s: #遍历s中的每个字母
10-
if num == j: #如果这个字母和num一样
11-
count += 1 #计数+1
12-
else:
13-
temp += str(count)+str(num) #存下“几”个“什么数”
14-
num = j #num改成当前数字
15-
count = 1 #计数回到1
16-
temp += str(count)+str(num) #加的是最后一个
17-
s = temp
1+
class Solution:
2+
def countAndSay(self, n: int) -> str:
3+
s = '1'
4+
for _ in range(n - 1):
5+
i = 0
6+
t = []
7+
while i < len(s):
8+
j = i
9+
while j < len(s) and s[j] == s[i]:
10+
j += 1
11+
t.append(str(j - i))
12+
t.append(str(s[i]))
13+
i = j
14+
s = ''.join(t)
1815
return s

0 commit comments

Comments
 (0)