# leetcode 46-Permutations and 47-Permutations II

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<vector<int>> permute(vector<int>& nums) {
vector<vector<int>>v;
v.clear();
vector<int>a;
next_c(v,nums.size(),a,nums,0);
return v;
}
void next_c(vector<vector<int>>&v,int n,vector<int>& a,vector<int>& b,int cur) /// b[]中的数可以相同
{
if(cur==n){
v.push_back(a);
}
else{
for(int i=0;i<n;i++)
if(!i||b[i]!=b[i-1])///
{
int c1=0,c2=0;
for(int j=0;j<cur;j++) if(a[j]==b[i]) c1++;
for(int j=0;j<n;j++) if(b[j]==b[i]) c2++;
if(c1<c2){
a.push_back(b[i]);// 不能用a[cur]=b[i];
next_c(v,n,a,b,cur+1);
a.pop_back();
}
}
}
}
};

class Solution {
public:
vector<vector<int>>v;
vector<vector<int>> permuteUnique(vector<int>& nums) {
v.clear();
vector<int>a;a.clear();
sort(nums.begin(),nums.end());
next_c(nums.size(),a,nums,0);
return v;
}
void next_c(int n,vector<int>& a,vector<int>& b,int cur) /// b[]中的数可以相同
{
if(cur==n){
v.push_back(a);
}
else{
for(int i=0;i<n;i++)
if(!i||b[i]!=b[i-1])///
{
int c1=0,c2=0;
for(int j=0;j<cur;j++) if(a[j]==b[i]) c1++;
for(int j=0;j<n;j++) if(b[j]==b[i]) c2++;
if(c1<c2){
a.push_back(b[i]); //a[cur]=b[i];
next_c(n,a,b,cur+1);
a.pop_back();
}
}
}
}
};

#### leetcode 229: Majority Element II

2015-07-07 04:40:19

#### Leetcode 119. Pascal's Triangle II 杨辉三角2 解题报告

2016-10-06 14:04:30

#### LeetCode 140. Word Break II（单词切分）

2016-05-27 00:22:25

#### leetcode 729与731. My Calendar II的通用解题思路

2017-11-21 16:29:13

#### 【LeetCode-面试算法经典-Java实现】【119-Pascal's Triangle II（帕斯卡三角形（杨辉三角）II）】

2015-08-13 06:21:37

#### [leetcode] Reverse Linked List II 链表反转

2016-07-16 21:31:01

#### [leetcode]47. Permutations II,python实现

2016-06-16 16:24:48

#### LeetCode 350. Intersection of Two Arrays II（数组交集）

2016-05-25 01:35:00

#### [LeetCode 253] Meeting Rooms II 扫描线的应用(Python)

2017-08-04 10:23:00

#### [LeetCode] Intersection of Two Arrays II 两个数组的交集II

2017-03-26 15:23:00