Skip to content

Commit 5867bfd

Browse files
committed
day 4 reversal of string added
1 parent 54761cb commit 5867bfd

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

june_challenge/4_reverse_string.cpp

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

0 commit comments

Comments
 (0)