LeetCode 344. Reverse String #80
quinnwencn
started this conversation in
General
Replies: 1 comment
-
AnalysisSolution written in Cppclass Solution {
public:
void reverseString(vector<char>& s) {
int left = 0, right = s.size() - 1;
while (left < right) {
std::swap(s[left], s[right]);
left++;
right--;
}
}
};Solution written in Rustimpl Solution {
pub fn reverse_string(s: &mut Vec<char>) {
s.reverse();
}
} |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Description
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.
Example 1:
Input: s = ["h","e","l","l","o"]
Output: ["o","l","l","e","h"]
Example 2:
Input: s = ["H","a","n","n","a","h"]
Output: ["h","a","n","n","a","H"]
Constraints:
1 <= s.length <= 105
s[i] is a printable ascii character.
Beta Was this translation helpful? Give feedback.
All reactions