Skip to content

Commit 518d5d8

Browse files
Add recursively reverse vector algorithm
1 parent 370ba7c commit 518d5d8

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

Algorithms/reverseStrRecursive.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution {
2+
void swap(char & a, char & b) {
3+
char _tmp = a;
4+
a = b;
5+
b = _tmp;
6+
}
7+
8+
9+
void help(vector<char> & s, int left, int right) {
10+
if(left >= right)
11+
return;
12+
13+
swap(s[left], s[right]);
14+
15+
help(s, ++left, --right);
16+
}
17+
18+
public:
19+
void reverseString(vector<char>& s) {
20+
help(s, 0, s.size()-1);
21+
}
22+
};

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,9 @@ This repository is for small C and C++ programs I do outside of my school work.
110110
- reverse only the characters in the string
111111
- Leetcode problem found [here](https://leetcode.com/problems/reverse-only-letters/)
112112
- reverseStr.cpp
113+
- reverseStrRecursive.cpp
114+
- recursively reverse vector
115+
- Leetcode problem found [here](https://leetcode.com/explore/learn/card/recursion-i/250/principle-of-recursion/1440/)
113116
- romanToDecimal.cpp
114117
- given roman numerals, return a decimal number
115118
- sortArrayByParity.cpp

0 commit comments

Comments
 (0)