提交 b1ad080a 编写于 作者: T tianzhongwei 提交者: labuladong

Update 子集排列组合.md

添加对应的C++版本
上级 38bc024a
......@@ -199,7 +199,7 @@ vector<vector<int>> permute(vector<int>& nums);
![](../pictures/子集/3.jpg)
我们当时使用 Java 代码写的解法:
[labuladong](https://github.com/labuladong) 提供Java解法代码:
```java
List<List<Integer>> res = new LinkedList<>();
......@@ -231,6 +231,44 @@ void backtrack(int[] nums, LinkedList<Integer> track) {
}
}
```
[renxiaoyao](https://github.com/tianzhongwei) 提供C++解法代码:
```C++
class Solution {
public:
vector<vector<int>> permute(vector<int>& nums) {
paths.clear();
path.clear();
vector<int> used(nums.size(),false);
helper(nums,used);
return paths;
}
private:
void helper(vector<int>& nums,vector<int>& used) {
if(path.size() == nums.size()) {
paths.push_back(path);
return ;
}
for(int i = 0 ; i < nums.size() ; ++i) {
if(used[i]) continue;
used[i] = true;
path.push_back(nums[i]);
helper(nums,used);
path.pop_back();
used[i] = false;
}
}
private:
vector<vector<int>> paths;
vector<int> path;
};
```
回溯模板依然没有变,但是根据排列问题和组合问题画出的树来看,排列问题的树比较对称,而组合问题的树越靠右节点越少。
......@@ -255,4 +293,4 @@ void backtrack(int[] nums, LinkedList<Integer> track) {
[下一篇:二分查找详解](../算法思维系列/二分查找详解.md)
[目录](../README.md#目录)
\ No newline at end of file
[目录](../README.md#目录)
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册