Permutations
Given a collection of 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]
, and [3,2,1]
.
Permutations II
Given a collection of numbers that might contain duplicates, return all possible unique permutations.
For example,
[1,1,2]
have the following unique permutations:[1,1,2]
, [1,2,1]
, and [2,1,1]
.
全排列问题
第二个题目基于第一题。第二题给的数组中还有可能相等的元素。
请參照
题一代码
class Solution {public: vector> permute(vector & nums) { vector >v; v.clear(); vector a; next_c(v,nums.size(),a,nums,0); return v; } void next_c(vector >&v,int n,vector & a,vector & b,int cur) /// b[]中的数能够同样 { if(cur==n){ v.push_back(a); } else{ for(int i=0;i
题二代码。当然能够过题一。
class Solution {public: vector>v; vector > permuteUnique(vector & nums) { v.clear(); vector a;a.clear(); sort(nums.begin(),nums.end()); next_c(nums.size(),a,nums,0); return v; } void next_c(int n,vector & a,vector & b,int cur) /// b[]中的数能够同样{ if(cur==n){ v.push_back(a); } else{ for(int i=0;i