Leetcodes Solution 46 Permutations
匯總
雪之下雪乃:leetcode解題總匯Given a collection of distinct numbers, return all possible permutations.
For example,
[1,2,3]
have the following permutations:
[ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1]]
思路1
遞歸
class Solution{public: vector<vector<int>> permute(vector<int>& nums){ vector<vector<int>> result; permuteRecursive(nums, 0, result); return result; } void permuteRecursive(vector<int> &nums, int begin, vector<vector<int>> &result){ if(begin >= nums.size()){ result.push_back(nums); return; } for(int i = begin; i < nums.size(); i++){ swap(nums[begin], nums[i]); permuteRecursive(nums, begin + 1, result); swap(nums[begin], nums[i]); } }};
推薦閱讀:
※浙江大學-數據結構-選講Complete Binary Search Tree-7.3.3
※leetcode解題總匯
※Leetcodes Solution 30 Substring with Concatenation of All Words
※Python數據結構與演算法刷題(1)——害死人不償命的(3n+1)猜想
※浙江大學-數據結構-小白專場:C語言實現如何建立圖-6.5.3