【leetcode】344. 反转字符串
c++版本:
class Solution {
public:
void reverseString(vector<char>& s) {
//定义数组头指针
int i = 0;
//定义数组尾指针
int j = s.size() - 1;
while(i < j){
//交换头尾指针元素
char temp = s[i];
s[i] = s[j];
s[j] = temp;
//头指针后移
i++;
//尾指针前移
j--;
}
}
};
class Solution {
public:
void reverseString(vector<char>& s) {
int l = 0, r = s.size() - 1;
while(l < r){
swap(s[l], s[r]);
l++; r--;
}
}
};
python版本:
class Solution:
def reverseString(self, s: List[str]) -> None:
"""
Do not return anything, modify s in-place instead.
"""
for i in range(len(s)//2):
s[i], s[len(s) - 1 - i] = s[len(s) - 1 - i], s[i]