File tree Expand file tree Collapse file tree 1 file changed +40
-2
lines changed Expand file tree Collapse file tree 1 file changed +40
-2
lines changed Original file line number Diff line number Diff line change @@ -199,7 +199,7 @@ vector<vector<int>> permute(vector<int>& nums);
199199![ ] ( ../pictures/子集/3.jpg )
200200
201201我们当时使用 Java 代码写的解法:
202-
202+ [ labuladong ] ( https://github.com/labuladong ) 提供Java解法代码:
203203``` java
204204List<List<Integer > > res = new LinkedList<> ();
205205
@@ -231,6 +231,44 @@ void backtrack(int[] nums, LinkedList<Integer> track) {
231231 }
232232}
233233```
234+ [ renxiaoyao] ( https://github.com/tianzhongwei ) 提供C++解法代码:
235+ ``` C++
236+ class Solution {
237+ public:
238+ vector<vector<int >> permute(vector<int >& nums) {
239+ paths.clear();
240+ path.clear();
241+
242+ vector<int> used(nums.size(),false);
243+
244+ helper(nums,used);
245+
246+ return paths;
247+ }
248+ private:
249+ void helper (vector<int >& nums,vector<int >& used) {
250+ if(path.size() == nums.size()) {
251+ paths.push_back(path);
252+ return ;
253+ }
254+
255+ for(int i = 0 ; i < nums.size() ; ++i) {
256+ if(used[i]) continue;
257+
258+ used[i] = true;
259+ path.push_back(nums[i]);
260+
261+ helper(nums,used);
262+
263+ path.pop_back();
264+ used[i] = false;
265+ }
266+ }
267+ private:
268+ vector<vector<int >> paths;
269+ vector<int > path;
270+ };
271+ ```
234272
235273回溯模板依然没有变,但是根据排列问题和组合问题画出的树来看,排列问题的树比较对称,而组合问题的树越靠右节点越少。
236274
@@ -255,4 +293,4 @@ void backtrack(int[] nums, LinkedList<Integer> track) {
255293
256294[下一篇:二分查找详解](../算法思维系列/二分查找详解.md)
257295
258- [ 目录] ( ../README.md#目录 )
296+ [目录](../README.md#目录)
You can’t perform that action at this time.
0 commit comments