leetcoder 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();
            }
        }
    }
}
};

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-11-03 21:26:02

leetcoder 46-Permutations and 47-Permutations II的相关文章

[Lintcode]16. Permutations II/[Leetcode]47. Permutations II

16. Permutations II/47. Permutations II 本题难度: Medium Topic: Search & Recursion Description Given a list of numbers with duplicate number in it. Find all unique permutations. Example Example 1: Input: [1,1] Output: [ [1,1]] Example 2: Input: [1,2,2] O

[leetcode] 47. 全排列 II

47. 全排列 II 比上一个题多了个重复性 与46. 全排列完全一样的代码... class Solution { // 当没有下一个排列时return false public boolean nextPermutation(int[] nums) { if (nums.length == 1) { return false; } int p = -1; for (int i = nums.length - 2; i >= 0; i--) { if (nums[i] < nums[i +

[LeetCode 46 &amp; 47] Permutations I &amp; II

题目链接:permutations 相似题型: 1. [LeetCode 39&40] Combination Sum I & II 2. [LeetCode 78] Subsets 3. [LeetCode 90] Subsets II 4. [LeetCode 22] Generate Parentheses 5. [LeetCode 77] Combinations import java.util.ArrayList; import java.util.List; /** * Gi

LeetCode46,47 Permutations, Permutations II

题目: LeetCode46 I Given a collection of distinct numbers, return all possible permutations. (Medium) 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] ] LeetCode47 II Given a collection of numb

leetCode 47.Permutations II (排列组合II) 解题思路和方法

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]. 思路:这题相比于上一题,是去除了反复项. 代码上与上题略有区别.详细代码例如以下

47. Permutations II java solutions

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], [2,1,1] ] 1 public class Solution { 2 List<List<Integer>> ans

47. 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]. 链接: http://leetcode.com/problems/permutations-ii/ 4/15

LeetCode 【47. 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], [2,1,1] ] 思路1.这题与Permutations的区别在于他允许重复数字,最简单的就是利用1的结果,保存在一个set中,去重复

47. Permutations II (java )

题目: Given a collection of numbers that might contain duplicates, return all possible unique permutations. 解析: 编码:

[leedcode 47] 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]. public class Solution { List<Integer> seq; List<List&l