力扣 LeetCode 344. 反转字符串(Day4:字符串)
解题思路:
双指针从左右两侧向中间移动
while版:
class Solution {
public void reverseString(char[] s) {
int l = 0, r = s.length - 1;
while (l < r) {
char tmp = s[l];
s[l] = s[r];
s[r] = tmp;
l++;
r--;
}
}
}
for版
class Solution {
public void reverseString(char[] s) {
for (int i = 0, j = s.length - 1; i < s.length / 2; i++, j--) {
char tmp = s[i];
s[i] = s[j];
s[j] = tmp;
}
}
}