LeetCode Solutions
344. Reverse String
Time: $O(n)$ Space: $O(1)$
class Solution {
public:
void reverseString(vector<char>& s) {
int l = 0;
int r = s.size() - 1;
while (l < r)
swap(s[l++], s[r--]);
}
};
class Solution {
public void reverseString(char[] s) {
int l = 0;
int r = s.length - 1;
while (l < r) {
char temp = s[l];
s[l++] = s[r];
s[r--] = temp;
}
}
}
class Solution:
def reverseString(self, s: List[str]) -> None:
l = 0
r = len(s) - 1
while l < r:
s[l], s[r] = s[r], s[l]
l += 1
r -= 1