File tree 6 files changed +223
-56
lines changed
solution/0000-0099/0038.Count and Say
6 files changed +223
-56
lines changed Original file line number Diff line number Diff line change @@ -69,7 +69,6 @@ countAndSay(4) = 读 "21" = 一 个 2 + 一 个 1 = "12" + "11" = "1211"
69
69
<li><code>1 <= n <= 30</code></li>
70
70
</ul >
71
71
72
-
73
72
## 解法
74
73
75
74
<!-- 这里可写通用的实现逻辑 -->
@@ -81,15 +80,95 @@ countAndSay(4) = 读 "21" = 一 个 2 + 一 个 1 = "12" + "11" = "1211"
81
80
<!-- 这里可写当前语言的特殊实现逻辑 -->
82
81
83
82
``` 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
85
98
```
86
99
87
100
### ** Java**
88
101
89
102
<!-- 这里可写当前语言的特殊实现逻辑 -->
90
103
91
104
``` 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
+ ```
92
150
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
+ }
93
172
```
94
173
95
174
### ** ...**
Original file line number Diff line number Diff line change @@ -45,21 +45,100 @@ countAndSay(4) = say "21" = one 2 + one 1 = "12" + "11&
45
45
<li><code>1 <= n <= 30</code></li>
46
46
</ul >
47
47
48
-
49
48
## Solutions
50
49
51
50
<!-- tabs:start -->
52
51
53
52
### ** Python3**
54
53
55
54
``` 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
57
70
```
58
71
59
72
### ** Java**
60
73
61
74
``` 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
+ ```
62
120
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
+ }
63
142
```
64
143
65
144
### ** ...**
Original file line number Diff line number Diff line change
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 number Diff line number Diff line change 1
1
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 ++
16
10
}
11
+ t .WriteString (strconv .Itoa (j - i ))
12
+ t .WriteByte (s [i ])
13
+ i = j
17
14
}
18
- buf .WriteByte (byte (48 + count ))
19
- buf .WriteString (c )
15
+ s = t .String ()
20
16
}
21
- return buf . String ()
22
- }
17
+ return s
18
+ }
Original file line number Diff line number Diff line change 1
1
class Solution {
2
2
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 ;
19
14
}
15
+ s = t .toString ();
20
16
}
21
- return sBuilder . toString () ;
17
+ return s ;
22
18
}
23
19
}
Original file line number Diff line number Diff line change 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 )
18
15
return s
You can’t perform that action at this time.
0 commit comments