Skip to content

Commit 8af13c6

Browse files
committed
1
1 parent 6082844 commit 8af13c6

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

Permutations/Permutations.cpp

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
public:
3+
vector<vector<int>> permute(vector<int>& num) {
4+
// Start typing your C/C++ solution below
5+
// DO NOT write int main() function
6+
7+
vector<vector<int>> result;
8+
dfs(num, 0, result);
9+
return move(result);
10+
}
11+
12+
void dfs(vector<int>& num, int step, vector<vector<int>>& result) {
13+
if (step == num.size()) {
14+
result.push_back(num);
15+
return;
16+
}
17+
for (int i = step; i < num.size(); i++) {
18+
swap(num[step], num[i]);
19+
dfs(num, step + 1, result);
20+
swap(num[i], num[step]);
21+
}
22+
}
23+
};

0 commit comments

Comments
 (0)