344. Reverse String

问题简述
Write a function that reverses a string. The input string is given as an array of characters s.

You must do this by modifying the input array in-place with O(1) extra memory.

双指针,同时更新并交换两个数值。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Solution {
public void reverseString(char[] s) {
int i = 0;
int j = s.length - 1;

while( i < j ) {
char temp = s[i];
s[i] = s[j];
s[j] = temp;

i++;
j--;
}
}
}

Author

Xander

Posted on

2022-04-05

Updated on

2022-04-05

Licensed under

Comments