File tree Expand file tree Collapse file tree 1 file changed +6
-11
lines changed
Expand file tree Collapse file tree 1 file changed +6
-11
lines changed Original file line number Diff line number Diff line change @@ -114,14 +114,11 @@ private:
114114```java
115115public class Solution {
116116 public String countAndSay(int n) {
117- if (n <= 0) return "";
118-
117+ assert n > 0;
119118 StringBuilder currSeq = new StringBuilder("1");
120-
121119 for (int i = 1; i < n; i++) {
122120 currSeq = getNextSeq(currSeq);
123121 }
124-
125122 return currSeq.toString();
126123 }
127124
@@ -132,8 +129,7 @@ public class Solution {
132129 if (i + 1 < seq.length() && seq.charAt(i) == seq.charAt(i + 1)) {
133130 cnt++;
134131 } else {
135- nextSeq.append(cnt);
136- nextSeq.append(seq.charAt(i));
132+ nextSeq.append(cnt).append(seq.charAt(i));
137133 cnt = 1;
138134 }
139135 }
@@ -150,7 +146,7 @@ public class Solution {
150146
151147略,与选用的数据结构有关。
152148
153- ### 题解2 - 递归
149+ ## 题解2 - 递归
154150
155151注意递归终止条件即可,核心过程差不多。
156152
@@ -213,7 +209,7 @@ public:
213209``` java
214210public class Solution {
215211 public String countAndSay (int n ) {
216- if (n <= 0 ) return " " ;
212+ assert n > 0 ;
217213 if (n == 1 ) return " 1" ;
218214
219215 String seq = countAndSay(n - 1 );
@@ -223,8 +219,7 @@ public class Solution {
223219 if (i + 1 < seq. length() && seq. charAt(i) == seq. charAt(i + 1 )) {
224220 cnt++ ;
225221 } else {
226- nextSeq. append(cnt);
227- nextSeq. append(seq. charAt(i));
222+ nextSeq. append(cnt). append(seq. charAt(i));
228223 cnt = 1 ;
229224 }
230225 }
@@ -236,7 +231,7 @@ public class Solution {
236231
237232### 源码分析
238233
239- 略
234+ 判断相邻字符是否相同时需要判断索引是否越界。
240235
241236### 复杂度分析
242237
You can’t perform that action at this time.
0 commit comments