diff --git a/problems/0344.反转字符串.md b/problems/0344.反转字符串.md index f5b0e22b..2807d04a 100644 --- a/problems/0344.反转字符串.md +++ b/problems/0344.反转字符串.md @@ -151,6 +151,23 @@ class Solution { } } } + +// 第二种方法用temp来交换数值更多人容易理解些 +class Solution { + public void reverseString(char[] s) { + int l = 0; + int r = s.length - 1; + while(l < r){ + char temp = s[l]; + s[l] = s[r]; + s[r] = temp; + l++; + r--; + } + } +} + + ``` Python: @@ -335,3 +352,4 @@ object Solution { + diff --git a/problems/0541.反转字符串II.md b/problems/0541.反转字符串II.md index 238e6349..59891365 100644 --- a/problems/0541.反转字符串II.md +++ b/problems/0541.反转字符串II.md @@ -194,6 +194,29 @@ class Solution { return new String(ch); } } + + +// 解法二还可以用temp来交换数值,会的人更多些 +class Solution { + public String reverseStr(String s, int k) { + char[] ch = s.toCharArray(); + for(int i = 0;i < ch.length;i += 2 * k){ + int start = i; + // 判断尾数够不够k个来取决end指针的位置 + int end = Math.min(ch.length - 1,start + k - 1); + while(start < end){ + + char temp = ch[start]; + ch[start] = ch[end]; + ch[end] = temp; + + start++; + end--; + } + } + return new String(ch); + } +} ``` ```java // 解法3 @@ -469,3 +492,4 @@ impl Solution { +