Skip to content

Commit

Permalink
Reverse a string
Browse files Browse the repository at this point in the history
  • Loading branch information
herrera-ignacio committed Jul 19, 2024
1 parent 380dce8 commit 5a8d48f
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
__pycache__
.idea
node_modules
.DS_Store
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,11 @@
- [Rod Cutting](problems/rod-cutting)
- [Set-covering problem](problems/set-covering/README.md)

### Others
### By Data Structure

#### Lists

- [Reverse a string](problems/others/lists/reverse-a-string)
- [Round-robin array of iterators](problems/others/round-robin-iterators/README.md)

## Coding platforms
Expand Down
17 changes: 17 additions & 0 deletions problems/others/lists/reverse-a-string/solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include <vector>
using namespace std;

class Solution {
public:
void reverseString(vector<char>& s) {
int left = 0;
int right = s.size() - 1;
while (left < right) {
char temp = s[left];
s[left] = s[right];
s[right] = temp;
left++;
right--;
}
}
};
14 changes: 14 additions & 0 deletions problems/others/lists/reverse-a-string/solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Solution:
def reverseString(self, s: List[str]) -> None:
"""
Do not return anything, modify s in-place instead.
"""
left = 0
right = len(s) - 1

while left < right:
s[left], s[right] = s[right], s[left]
left += 1
right -= 1


0 comments on commit 5a8d48f

Please sign in to comment.