Reverse String
描述
Write a function to reverse a string. The input string is given as an array of characters.
You must do this by modifying the input array in-place with O(1)
extra memory.
分析
无。
代码
- Java
- C++
class Solution {
public void reverseString(char[] s) {
int left = 0, right = s.length - 1;
while (left < right) {
char temp = s[left];
s[left] = s[right];
s[right] = temp;
left++;
right--;
}
}
}
// Reverse String
// 时间复杂度O(n),空间复杂度O(1)
class Solution {
public:
void reverseString(vector<char>& s) {
int left = 0, right = s.size() - 1;
while (left < right) {
swap(s[left], s[right]);
left++;
right--;
}
}
};