344. 反转字符串

题目链接:https://leetcode-cn.com/problems/reverse-string/
执行用时:12 ms, 在所有 C++ 提交中击败了99.55% 的用户
内存消耗:22.7 MB, 在所有 C++ 提交中击败了5.09% 的用户

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Solution {
public:
void reverseString(vector<char>& s) {
int left =0, right = s.size()-1;
char t;
while (left<right){
t=s[left];
s[left] = s[right];
s[right]=t;
left++;
right--;
}
}
};

执行用时:1 ms, 在所有 Java 提交中击败了100.00% 的用户
内存消耗:45.1 MB, 在所有 Java 提交中击败了58.14% 的用户

1
2
3
4
5
6
7
8
9
10
11
12
13
class Solution {
public void reverseString(char[] s) {
int left =0, right = s.length-1;
char t;
while (left < right){
t = s[left];
s[left] = s[right];
s[right] = t;
left++;
right--;
}
}
}

执行用时:156 ms, 在所有 Swift 提交中击败了98.87% 的用户
内存消耗:18.1 MB, 在所有 Swift 提交中击败了27.44% 的用户

1
2
3
4
5
6
7
8
9
10
11
12
13
class Solution {
func reverseString(_ s: inout [Character]) {
var left = 0, right = s.count - 1
var t : Character
while left < right {
t = s[left]
s[left] = s[right]
s[right] = t
left += 1
right -= 1
}
}
}

344. 反转字符串
https://pisces34.github.io/2021/06/15/leetcode/344reverseStr/
发布于
2021年6月15日
许可协议