Skip to content

Commit

Permalink
Create ReverseString.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
priyavelu7 authored Jun 4, 2020
1 parent 6c2c7f3 commit 3c834bd
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions Week 1/ReverseString.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
Write a function that reverses a string. The input string is given as an array of characters char[].
Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.
You may assume all the characters consist of printable ascii characters.
Example 1:
Input: ["h","e","l","l","o"]
Output: ["o","l","l","e","h"]
*/
class Solution {
public:
void reverseString(vector<char>& s) {
int start=0,end=s.size()-1;
while(start<end){
swap(s[start],s[end]);
start++;
end--;
}
}
};

0 comments on commit 3c834bd

Please sign in to comment.