diff options
Diffstat (limited to 'store/works/solutions/leetcode/cpp/46.cpp')
-rw-r--r-- | store/works/solutions/leetcode/cpp/46.cpp | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/store/works/solutions/leetcode/cpp/46.cpp b/store/works/solutions/leetcode/cpp/46.cpp new file mode 100644 index 0000000..bfb3c83 --- /dev/null +++ b/store/works/solutions/leetcode/cpp/46.cpp @@ -0,0 +1,34 @@ +#include <vector> + +using std::vector; + +class Solution { +public: + void dfs(const vector<int> &nums, vector<int> ¤t, 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; + } +}; |