summaryrefslogtreecommitdiff
path: root/cpp/46.cpp
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2021-02-22 23:20:22 +0800
committercrupest <crupest@outlook.com>2021-02-22 23:20:22 +0800
commitbd5d833017c06e7d338c957bfaf09df081ea6267 (patch)
tree2d3b9d0efbb04bfe48ae7acf2d324460c4fb5110 /cpp/46.cpp
parentc26711c412f92ea348a8f40e92ca8535aa7725e9 (diff)
parent0c90a62c35d3f4f03f9d8fb55b06a9bbb658dcbc (diff)
downloadsolutions-bd5d833017c06e7d338c957bfaf09df081ea6267.tar.gz
solutions-bd5d833017c06e7d338c957bfaf09df081ea6267.tar.bz2
solutions-bd5d833017c06e7d338c957bfaf09df081ea6267.zip
Merge branch 'master' of https://github.com/crupest/leetcode
Diffstat (limited to 'cpp/46.cpp')
-rw-r--r--cpp/46.cpp34
1 files changed, 34 insertions, 0 deletions
diff --git a/cpp/46.cpp b/cpp/46.cpp
new file mode 100644
index 0000000..bfb3c83
--- /dev/null
+++ b/cpp/46.cpp
@@ -0,0 +1,34 @@
+#include <vector>
+
+using std::vector;
+
+class Solution {
+public:
+ void dfs(const vector<int> &nums, vector<int> &current, bool *num_used,
+ vector<vector<int>> &result) {
+ if (current.size() == nums.size()) {
+ result.push_back(current);
+ return;
+ }
+
+ for (int i = 0; i < nums.size(); i++) {
+ if (!num_used[i]) {
+ num_used[i] = true;
+ current.push_back(nums[i]);
+ dfs(nums, current, num_used, result);
+ current.pop_back();
+ num_used[i] = false;
+ }
+ }
+ }
+
+ vector<vector<int>> permute(vector<int> &nums) {
+ vector<int> current;
+ vector<vector<int>> result;
+ bool *num_used = new bool[nums.size()]{false};
+ dfs(nums, current, num_used, result);
+ delete[] num_used;
+
+ return result;
+ }
+};