Skip to content

Commit 3796b8a

Browse files
Add C++ implementation
Signed-off-by: begeekmyfriend <[email protected]>
1 parent 158c1ff commit 3796b8a

File tree

3 files changed

+64
-4
lines changed

3 files changed

+64
-4
lines changed
+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#include <iostream>
2+
#include <string>
3+
#include <vector>
4+
5+
6+
using namespace std;
7+
8+
class Solution {
9+
public:
10+
vector<vector<int>> young_tableau(int n) {
11+
vector<vector<int>> res;
12+
dfs(n, 0, 0, res);
13+
return res;
14+
}
15+
16+
private:
17+
vector<char> stack;
18+
19+
void dfs(int n, int l, int r, vector<vector<int>>& res) {
20+
if (stack.size() == 2 * n) {
21+
vector<int> sol(2 * n);
22+
for (int i = 0, j = 0, k = n; i < 2 * n; i++) {
23+
if (stack[i] == '(') {
24+
sol[j++] = i + 1;
25+
} else {
26+
sol[k++] = i + 1;
27+
}
28+
}
29+
res.push_back(sol);
30+
} else {
31+
if (l < n) {
32+
stack.push_back('(');
33+
dfs(n, l + 1, r, res);
34+
stack.pop_back();
35+
}
36+
37+
if (r < l) {
38+
stack.push_back(')');
39+
dfs(n, l, r + 1, res);
40+
stack.pop_back();
41+
}
42+
}
43+
}
44+
};
45+
46+
int main(int argc, char **argv)
47+
{
48+
int n = atoi(argv[1]);
49+
Solution *solution = new Solution();
50+
vector<vector<int>> res = solution->young_tableau(n);
51+
for (auto& v : res) {
52+
for (int i = 0; i < 2 * n; i++) {
53+
if (i == n) cout << endl;
54+
cout << v[i] << ' ';
55+
}
56+
cout << endl;
57+
}
58+
59+
return 0;
60+
}

0048_rotate_image/rotate.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@ class Solution {
1515
matrix[size - 1 - i][size - 1 - j] = matrix[j][size - 1 - i];
1616
matrix[j][size - 1 - i] = tmp;
1717
}
18-
}
18+
}
1919
}
2020
};

0322_coin_change/coin_change.cc

+3-3
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class Solution {
1919
#else
2020
// BFS solution is slow...
2121
queue<int> q;
22-
unordered_set<int> s;
22+
unordered_set<int> visited;
2323
int step = 0;
2424
q.push(amount);
2525
while (!q.empty()) {
@@ -31,8 +31,8 @@ class Solution {
3131
for (int coin : coins) {
3232
int n = q.front() - coin;
3333
if (n >= 0) {
34-
if (s.count(n) == 0) {
35-
s.insert(n);
34+
if (visited.count(n) == 0) {
35+
visited.insert(n);
3636
q.push(n);
3737
}
3838
}

0 commit comments

Comments
 (0)