344. 反转字符串
一、Java
class Solution {public void reverseString(char[] s) {for (int l = 0, r = s.length - 1; l < r; l++, r--) {s[l] ^= s[r];s[r] ^= s[l];s[l] ^= s[r];}}
}
二、C++
#include <vector>
using namespace std;
class Solution {
public:void reverseString(vector<char>& s) {for(int l = 0, r = s.size() - 1; l < r; l++, r--) {s[l] ^= s[r];s[r] ^= s[l];s[l] ^= s[r];}}
};
三、Python
from typing import Listclass Solution:def reverseString(self, s: List[str]) -> None:l, r = 0, len(s) - 1while l < r:s[l], s[r] = s[r], s[l]l, r = l + 1, r - 1
四、JavaScript
var reverseString = function (s) {for (let l = 0, r = s.length - 1, t; l < r; l++, r--) {t = s[l];s[l] = s[r];s[r] = t;}
}
五、Go
package mainfunc reverseString(s []byte) {l := 0r := len(s) - 1for ; l < r; {s[l], s[r] = s[r], s[l]l++r--}}