[LeetCode] Reverse String

344. Reverse String

Write a function that reverses a string. The input string is given as an array of characters s.

You must do this by modifying the input array in-place with O(1) extra memory.

1
2
3
4
5
6
7
8
class Solution {
public:
void reverseString(vector<char>& s) {
for(int i = 0; i < s.size() / 2; i++) {
swap(s[i],s[s.size()-1-i]);
}
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/02/12/PS/LeetCode/reverse-string/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.