LC.P344[反转字符串]

方法一:双指针

1
2
3
4
5
6
7
8
9
class Solution {
public void reverseString(char[] s) {
for (int left = 0, right = s.length - 1; left < right; ++left, --right) {
char temp = s[left];
s[left] = s[right];
s[right] = temp;
}
}
}
  • 时间复杂度:$O(n)$
  • 空间复杂度:$O(1)$