diff --git a/.gitignore b/.gitignore index 67c758f..fd34257 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ __pycache__ .idea node_modules +.DS_Store \ No newline at end of file diff --git a/README.md b/README.md index 599913d..736c88a 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/problems/others/lists/reverse-a-string/solution.cpp b/problems/others/lists/reverse-a-string/solution.cpp new file mode 100644 index 0000000..e28ace3 --- /dev/null +++ b/problems/others/lists/reverse-a-string/solution.cpp @@ -0,0 +1,17 @@ +#include +using namespace std; + +class Solution { +public: + void reverseString(vector& 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--; + } + } +}; \ No newline at end of file diff --git a/problems/others/lists/reverse-a-string/solution.py b/problems/others/lists/reverse-a-string/solution.py new file mode 100644 index 0000000..9a9fb42 --- /dev/null +++ b/problems/others/lists/reverse-a-string/solution.py @@ -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 + +