1
+ <!-- MarkdownTOC -->
2
+
3
+ - [ 说明] ( #说明 )
4
+ - [ 1. KMP 算法] ( #1-kmp-算法 )
5
+ - [ 2. 替换空格] ( #2-替换空格 )
6
+ - [ 3. 最长公共前缀] ( #3-最长公共前缀 )
7
+ - [ 4. 回文串] ( #4-回文串 )
8
+ - [ 4.1. 最长回文串] ( #41-最长回文串 )
9
+ - [ 4.2. 验证回文串] ( #42-验证回文串 )
10
+ - [ 4.3. 最长回文子串] ( #43-最长回文子串 )
11
+ - [ 4.4. 最长回文子序列] ( #44-最长回文子序列 )
12
+ - [ 5. 括号匹配深度] ( #5-括号匹配深度 )
13
+ - [ 6. 把字符串转换成整数] ( #6-把字符串转换成整数 )
14
+
15
+ <!-- /MarkdownTOC -->
16
+
17
+
1
18
## 说明
2
19
3
20
- 本文作者:wwwxmu
36
53
// https://www.weiweiblog.cn/replacespace/
37
54
public class Solution {
38
55
39
- /**
40
- * 第一种方法:常规方法。利用String.charAt(i)以及String.valueOf(char).equals(" "
41
- * )遍历字符串并判断元素是否为空格。是则替换为"%20",否则不替换
42
- */
43
- public static String replaceSpace (StringBuffer str ) {
44
-
45
- int length = str. length();
46
- // System.out.println("length=" + length);
47
- StringBuffer result = new StringBuffer ();
48
- for (int i = 0 ; i < length; i++ ) {
49
- char b = str. charAt(i);
50
- if (String . valueOf(b). equals(" " )) {
51
- result. append(" %20" );
52
- } else {
53
- result. append(b);
54
- }
55
- }
56
- return result. toString();
57
-
58
- }
59
-
60
- /**
61
- * 第二种方法:利用API替换掉所用空格,一行代码解决问题
62
- */
63
- public static String replaceSpace2 (StringBuffer str ) {
64
-
65
- return str. toString(). replaceAll(" \\ s" , " %20" );
66
- }
56
+ /**
57
+ * 第一种方法:常规方法。利用String.charAt(i)以及String.valueOf(char).equals(" "
58
+ * )遍历字符串并判断元素是否为空格。是则替换为"%20",否则不替换
59
+ */
60
+ public static String replaceSpace (StringBuffer str ) {
61
+
62
+ int length = str. length();
63
+ // System.out.println("length=" + length);
64
+ StringBuffer result = new StringBuffer ();
65
+ for (int i = 0 ; i < length; i++ ) {
66
+ char b = str. charAt(i);
67
+ if (String . valueOf(b). equals(" " )) {
68
+ result. append(" %20" );
69
+ } else {
70
+ result. append(b);
71
+ }
72
+ }
73
+ return result. toString();
74
+
75
+ }
76
+
77
+ /**
78
+ * 第二种方法:利用API替换掉所用空格,一行代码解决问题
79
+ */
80
+ public static String replaceSpace2 (StringBuffer str ) {
81
+
82
+ return str. toString(). replaceAll(" \\ s" , " %20" );
83
+ }
67
84
}
68
85
69
86
```
@@ -93,36 +110,36 @@ public class Solution {
93
110
``` java
94
111
// https://leetcode-cn.com/problems/longest-common-prefix/description/
95
112
public class Main {
96
- public static String replaceSpace (String [] strs ) {
97
-
98
- // 数组长度
99
- int len = strs. length;
100
- // 用于保存结果
101
- StringBuffer res = new StringBuffer ();
102
- // 注意:=是赋值,==是判断
103
- if (strs == null || strs. length == 0 ) {
104
- return " " ;
105
- }
106
- // 给字符串数组的元素按照升序排序(包含数字的话,数字会排在前面)
107
- Arrays . sort(strs);
108
- int m = strs[0 ]. length();
109
- int n = strs[len - 1 ]. length();
110
- int num = Math . min(m, n);
111
- for (int i = 0 ; i < num; i++ ) {
112
- if (strs[0 ]. charAt(i) == strs[len - 1 ]. charAt(i)) {
113
- res. append(strs[0 ]. charAt(i));
114
- } else
115
- break ;
116
-
117
- }
118
- return res. toString();
119
-
120
- }
113
+ public static String replaceSpace (String [] strs ) {
114
+
115
+ // 数组长度
116
+ int len = strs. length;
117
+ // 用于保存结果
118
+ StringBuffer res = new StringBuffer ();
119
+ // 注意:=是赋值,==是判断
120
+ if (strs == null || strs. length == 0 ) {
121
+ return " " ;
122
+ }
123
+ // 给字符串数组的元素按照升序排序(包含数字的话,数字会排在前面)
124
+ Arrays . sort(strs);
125
+ int m = strs[0 ]. length();
126
+ int n = strs[len - 1 ]. length();
127
+ int num = Math . min(m, n);
128
+ for (int i = 0 ; i < num; i++ ) {
129
+ if (strs[0 ]. charAt(i) == strs[len - 1 ]. charAt(i)) {
130
+ res. append(strs[0 ]. charAt(i));
131
+ } else
132
+ break ;
133
+
134
+ }
135
+ return res. toString();
136
+
137
+ }
121
138
// 测试
122
- public static void main (String [] args ) {
123
- String [] strs = { " customer" , " car" , " cat" };
124
- System . out. println(Main . replaceSpace(strs));// c
125
- }
139
+ public static void main (String [] args ) {
140
+ String [] strs = { " customer" , " car" , " cat" };
141
+ System . out. println(Main . replaceSpace(strs));// c
142
+ }
126
143
}
127
144
128
145
```
@@ -161,23 +178,23 @@ public class Main {
161
178
``` java
162
179
// https://leetcode-cn.com/problems/longest-palindrome/description/
163
180
class Solution {
164
- public int longestPalindrome (String s ) {
165
- if (s. length() == 0 )
166
- return 0 ;
167
- // 用于存放字符
168
- HashSet<Character > hashset = new HashSet<Character > ();
169
- char [] chars = s. toCharArray();
170
- int count = 0 ;
171
- for (int i = 0 ; i < chars. length; i++ ) {
172
- if (! hashset. contains(chars[i])) {// 如果hashset没有该字符就保存进去
173
- hashset. add(chars[i]);
174
- } else {// 如果有,就让count++(说明找到了一个成对的字符),然后把该字符移除
175
- hashset. remove(chars[i]);
176
- count++ ;
177
- }
178
- }
179
- return hashset. isEmpty() ? count * 2 : count * 2 + 1 ;
180
- }
181
+ public int longestPalindrome (String s ) {
182
+ if (s. length() == 0 )
183
+ return 0 ;
184
+ // 用于存放字符
185
+ HashSet<Character > hashset = new HashSet<Character > ();
186
+ char [] chars = s. toCharArray();
187
+ int count = 0 ;
188
+ for (int i = 0 ; i < chars. length; i++ ) {
189
+ if (! hashset. contains(chars[i])) {// 如果hashset没有该字符就保存进去
190
+ hashset. add(chars[i]);
191
+ } else {// 如果有,就让count++(说明找到了一个成对的字符),然后把该字符移除
192
+ hashset. remove(chars[i]);
193
+ count++ ;
194
+ }
195
+ }
196
+ return hashset. isEmpty() ? count * 2 : count * 2 + 1 ;
197
+ }
181
198
}
182
199
```
183
200
@@ -203,26 +220,26 @@ class Solution {
203
220
``` java
204
221
// https://leetcode-cn.com/problems/valid-palindrome/description/
205
222
class Solution {
206
- public boolean isPalindrome (String s ) {
207
- if (s. length() == 0 )
208
- return true ;
209
- int l = 0 , r = s. length() - 1 ;
210
- while (l < r) {
211
- // 从头和尾开始向中间遍历
212
- if (! Character . isLetterOrDigit(s. charAt(l))) {// 字符不是字母和数字的情况
213
- l++ ;
214
- } else if (! Character . isLetterOrDigit(s. charAt(r))) {// 字符不是字母和数字的情况
215
- r-- ;
216
- } else {
217
- // 判断二者是否相等
218
- if (Character . toLowerCase(s. charAt(l)) != Character . toLowerCase(s. charAt(r)))
219
- return false ;
220
- l++ ;
221
- r-- ;
222
- }
223
- }
224
- return true ;
225
- }
223
+ public boolean isPalindrome (String s ) {
224
+ if (s. length() == 0 )
225
+ return true ;
226
+ int l = 0 , r = s. length() - 1 ;
227
+ while (l < r) {
228
+ // 从头和尾开始向中间遍历
229
+ if (! Character . isLetterOrDigit(s. charAt(l))) {// 字符不是字母和数字的情况
230
+ l++ ;
231
+ } else if (! Character . isLetterOrDigit(s. charAt(r))) {// 字符不是字母和数字的情况
232
+ r-- ;
233
+ } else {
234
+ // 判断二者是否相等
235
+ if (Character . toLowerCase(s. charAt(l)) != Character . toLowerCase(s. charAt(r)))
236
+ return false ;
237
+ l++ ;
238
+ r-- ;
239
+ }
240
+ }
241
+ return true ;
242
+ }
226
243
}
227
244
```
228
245
@@ -254,28 +271,28 @@ class Solution {
254
271
``` java
255
272
// https://leetcode-cn.com/problems/longest-palindromic-substring/description/
256
273
class Solution {
257
- private int index, len;
258
-
259
- public String longestPalindrome (String s ) {
260
- if (s. length() < 2 )
261
- return s;
262
- for (int i = 0 ; i < s. length() - 1 ; i++ ) {
263
- PalindromeHelper(s, i, i);
264
- PalindromeHelper(s, i, i + 1 );
265
- }
266
- return s. substring(index, index + len);
267
- }
268
-
269
- public void PalindromeHelper (String s , int l , int r ) {
270
- while (l >= 0 && r < s. length() && s. charAt(l) == s. charAt(r)) {
271
- l-- ;
272
- r++ ;
273
- }
274
- if (len < r - l - 1 ) {
275
- index = l + 1 ;
276
- len = r - l - 1 ;
277
- }
278
- }
274
+ private int index, len;
275
+
276
+ public String longestPalindrome (String s ) {
277
+ if (s. length() < 2 )
278
+ return s;
279
+ for (int i = 0 ; i < s. length() - 1 ; i++ ) {
280
+ PalindromeHelper(s, i, i);
281
+ PalindromeHelper(s, i, i + 1 );
282
+ }
283
+ return s. substring(index, index + len);
284
+ }
285
+
286
+ public void PalindromeHelper (String s , int l , int r ) {
287
+ while (l >= 0 && r < s. length() && s. charAt(l) == s. charAt(r)) {
288
+ l-- ;
289
+ r++ ;
290
+ }
291
+ if (len < r - l - 1 ) {
292
+ index = l + 1 ;
293
+ len = r - l - 1 ;
294
+ }
295
+ }
279
296
}
280
297
```
281
298
@@ -381,20 +398,20 @@ import java.util.Scanner;
381
398
* @Description: TODO 求给定合法括号序列的深度
382
399
*/
383
400
public class Main {
384
- public static void main (String [] args ) {
385
- Scanner sc = new Scanner (System . in);
386
- String s = sc. nextLine();
387
- int cnt = 0 , max = 0 , i;
388
- for (i = 0 ; i < s. length(); ++ i) {
389
- if (s. charAt(i) == ' (' )
390
- cnt++ ;
391
- else
392
- cnt-- ;
393
- max = Math . max(max, cnt);
394
- }
395
- sc. close();
396
- System . out. println(max);
397
- }
401
+ public static void main (String [] args ) {
402
+ Scanner sc = new Scanner (System . in);
403
+ String s = sc. nextLine();
404
+ int cnt = 0 , max = 0 , i;
405
+ for (i = 0 ; i < s. length(); ++ i) {
406
+ if (s. charAt(i) == ' (' )
407
+ cnt++ ;
408
+ else
409
+ cnt-- ;
410
+ max = Math . max(max, cnt);
411
+ }
412
+ sc. close();
413
+ System . out. println(max);
414
+ }
398
415
}
399
416
400
417
```
@@ -407,39 +424,39 @@ public class Main {
407
424
// https://www.weiweiblog.cn/strtoint/
408
425
public class Main {
409
426
410
- public static int StrToInt (String str ) {
411
- if (str. length() == 0 )
412
- return 0 ;
413
- char [] chars = str. toCharArray();
414
- // 判断是否存在符号位
415
- int flag = 0 ;
416
- if (chars[0 ] == ' +' )
417
- flag = 1 ;
418
- else if (chars[0 ] == ' -' )
419
- flag = 2 ;
420
- int start = flag > 0 ? 1 : 0 ;
421
- int res = 0 ;// 保存结果
422
- for (int i = start; i < chars. length; i++ ) {
423
- if (Character . isDigit(chars[i])) {// 调用Character.isDigit(char)方法判断是否是数字,是返回True,否则False
424
- int temp = chars[i] - ' 0' ;
425
- res = res * 10 + temp;
426
- } else {
427
- return 0 ;
428
- }
429
- }
430
- return flag == 1 ? res : - res;
431
-
432
- }
433
-
434
- public static void main (String [] args ) {
435
- // TODO Auto-generated method stub
436
- String s = " -12312312" ;
437
- System . out. println(" 使用库函数转换:" + Integer . valueOf(s));
438
- int res = Main . StrToInt(s);
439
- System . out. println(" 使用自己写的方法转换:" + res);
440
-
441
- }
427
+ public static int StrToInt (String str ) {
428
+ if (str. length() == 0 )
429
+ return 0 ;
430
+ char [] chars = str. toCharArray();
431
+ // 判断是否存在符号位
432
+ int flag = 0 ;
433
+ if (chars[0 ] == ' +' )
434
+ flag = 1 ;
435
+ else if (chars[0 ] == ' -' )
436
+ flag = 2 ;
437
+ int start = flag > 0 ? 1 : 0 ;
438
+ int res = 0 ;// 保存结果
439
+ for (int i = start; i < chars. length; i++ ) {
440
+ if (Character . isDigit(chars[i])) {// 调用Character.isDigit(char)方法判断是否是数字,是返回True,否则False
441
+ int temp = chars[i] - ' 0' ;
442
+ res = res * 10 + temp;
443
+ } else {
444
+ return 0 ;
445
+ }
446
+ }
447
+ return flag == 1 ? res : - res;
448
+
449
+ }
450
+
451
+ public static void main (String [] args ) {
452
+ // TODO Auto-generated method stub
453
+ String s = " -12312312" ;
454
+ System . out. println(" 使用库函数转换:" + Integer . valueOf(s));
455
+ int res = Main . StrToInt(s);
456
+ System . out. println(" 使用自己写的方法转换:" + res);
457
+
458
+ }
442
459
443
460
}
444
461
445
- ```
462
+ ```
0 commit comments