Skip to content

Commit 9adcc82

Browse files
Reverse String
1 parent 3cb46bf commit 9adcc82

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

4.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
3+
Reverse String
4+
--------------
5+
6+
Write a function that reverses a string. The input string is given as an array of characters char[].
7+
8+
Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.
9+
10+
You may assume all the characters consist of printable ascii characters.
11+
12+
13+
14+
Example 1:
15+
16+
Input: ["h","e","l","l","o"]
17+
Output: ["o","l","l","e","h"]
18+
Example 2:
19+
20+
Input: ["H","a","n","n","a","h"]
21+
Output: ["h","a","n","n","a","H"]
22+
23+
*/
24+
25+
class Solution {
26+
public:
27+
void reverseString(vector<char>& s) {
28+
int i = 0, j = s.size()-1;
29+
while(i<j) {
30+
char temp = s[i];
31+
s[i] = s[j];
32+
s[j] = temp;
33+
i++;
34+
j--;
35+
}
36+
}
37+
};

0 commit comments

Comments
 (0)