From 9a0c3251cffbc150e283faccb8da9d431a2586fe Mon Sep 17 00:00:00 2001 From: guide Date: Wed, 29 Dec 2021 21:06:14 +0800 Subject: [PATCH] =?UTF-8?q?Update=20=E5=87=A0=E9=81=93=E5=B8=B8=E8=A7=81?= =?UTF-8?q?=E7=9A=84=E5=AD=97=E7=AC=A6=E4=B8=B2=E7=AE=97=E6=B3=95=E9=A2=98?= =?UTF-8?q?.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../algorithms/几道常见的字符串算法题.md | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/docs/cs-basics/algorithms/几道常见的字符串算法题.md b/docs/cs-basics/algorithms/几道常见的字符串算法题.md index 37176650..ccd420c4 100644 --- a/docs/cs-basics/algorithms/几道常见的字符串算法题.md +++ b/docs/cs-basics/algorithms/几道常见的字符串算法题.md @@ -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: 编写一个函数来查找字符串数组中的最长公共前缀。如果不存在公共前缀,返回空字符串 ""。