1
0
mirror of https://github.com/Snailclimb/JavaGuide synced 2025-06-20 22:17:09 +08:00

Update 几道常见的字符串算法题.md

This commit is contained in:
guide 2021-12-29 21:06:14 +08:00
parent 671450a131
commit 9a0c3251cf

View File

@ -61,13 +61,19 @@ public class Solution {
* 第二种方法利用API替换掉所用空格一行代码解决问题
*/
public static String replaceSpace2(StringBuffer str) {
return str.toString().replaceAll("\\s", "%20");
}
}
```
对于替换固定字符(比如空格)的情况,第二种方法其实可以使用 `replace` 方法替换,性能更好!
```java
str.toString().replace(" ","%20");
```
## 3. 最长公共前缀
> Leetcode: 编写一个函数来查找字符串数组中的最长公共前缀。如果不存在公共前缀,返回空字符串 ""。