LeetCode344-反转字符串

LeetCode344-反转字符串

双指针实现O(1)

设置一个左指针,一个右指针,将左右两边进行交换

1
2
3
4
5
6
7
8
9
10
11
public void reverseString(char[] s) {
int l = 0, r = s.length - 1;
char temp;
while (l < r) {
temp = s[l];
s[l] = s[r];
s[r] = temp;
l++;
r--;
}
}

LeetCode344-反转字符串
https://www.gravity.wang/2022/03/31/leetcode344/
Author
Gravity
Posted on
March 31, 2022
Licensed under