一、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
- 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 List
-
-
- class Solution:
- def reverseString(self, s: List[str]) -> None:
- l, r = 0, len(s) - 1
- while 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 main
-
- func reverseString(s []byte) {
- l := 0
- r := len(s) - 1
- for ; l < r; {
- s[l], s[r] = s[r], s[l]
- l++
- r--
- }
-
- }