leetcode 46-Permutations and 47-Permutations II

原创 2015年07月09日 20:22:06

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 OJ> 47/46 Permutations (II / I)

47. Permutations II My Submissions Question Total Accepted: 57990 Total Submissions: 215694 Diff...

LeetCode 46. Permutations47. Permutations II&&131. Palindrome Partitioning(全排列问题)

LeetCode 46. Permutations Given a collection of distinct numbers, return all possible permutations...

LeetCode 46 Permutations II

Given a collection of numbers that might contain duplicates, return all possible unique permutations...

[LeetCode]--47. Permutations II

Given a collection of numbers that might contain duplicates, return all possible unique permutations...

Leetcode #47. Permutations II 全排列2 解题报告

1 解题思想这道题,就是第二个去全排列了,我昨天也预告过了,没看过的先看下上一题的解法: Leetcode # 46. Permutations 全排列 解题报告这道题有什么不一样的么?上一道题的...

[LeetCode47]Permutations II

Given a collection of numbers that might contain duplicates, return all possible unique permutations...

leetcode 47. Permutations II

import java.lang.reflect.Array; import java.util.ArrayList; import java.util.Arrays; import java.uti...

LeetCode#47. Permutations II

数组元素排列组合

LeetCode Algorithms 47. Permutations II

题目难度: Medium 原题描述: Given a collection of distinct numbers, return all possible permutations. For ...

leetcode - 47. Permutations II(全排列)

47. Permutations II Given a collection of numbers that mightcontain duplicates, return all possible ...
  • laeen
  • laeen
  • 2017-06-29 16:21
  • 104
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)