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 46-Permutations and 47-Permutations II的更多相关文章

  1. leetcode 46. 全排列 及 47. 全排列 II

    46. 全排列 问题描述 给定一个没有重复数字的序列,返回其所有可能的全排列. 示例: 输入: [1,2,3] 输出: [ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3 ...

  2. leetcode46. Permutations 、47. Permutations II、 剑指offer字符串的排列

    字符串排列和PermutationsII差不多 Permutations第一种解法: 这种方法从0开始遍历,通过visited来存储是否被访问到,level代表每次已经存储了多少个数字 class S ...

  3. LeetCode第[46]题(Java):Permutations(求所有全排列) 含扩展——第[47]题Permutations 2

    题目:求所有全排列 难度:Medium 题目内容: Given a collection of distinct integers, return all possible permutations. ...

  4. Leetcode之回溯法专题-47. 全排列 II(Permutations II)

    Leetcode之回溯法专题-47. 全排列 II(Permutations II) 给定一个可包含重复数字的序列,返回所有不重复的全排列. 示例: 输入: [1,1,2] 输出: [ [1,1,2] ...

  5. [Leetcode][Python]47: Permutations II

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 47: Permutations IIhttps://oj.leetcode. ...

  6. [LeetCode] 47. Permutations II 全排列 II

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

  7. [LeetCode] Backtracking Template for (Subsets, Permutations, and Combination Sum)

    根据issac3 用Java总结了backtracking template, 我用他的方法改成了Python. 以下为template. def backtrack(ans, temp, nums, ...

  8. [LeetCode] 47. 全排列 II

    题目链接 : https://leetcode-cn.com/problems/permutations-ii/ 题目描述: 给定一个可包含重复数字的序列,返回所有不重复的全排列. 示例: 输入: [ ...

  9. LeetCode 47——全排列 II

    1. 题目 2. 解答 在 LeetCode 46--全排列 中我们已经知道,全排列其实就是先确定某一个位置的元素,然后余下就是一个子问题.在那个问题中,数据没有重复,所以数据中的任意元素都可以放在最 ...

  10. Java实现 LeetCode 47 全排列 II(二)

    47. 全排列 II 给定一个可包含重复数字的序列,返回所有不重复的全排列. 示例: 输入: [1,1,2] 输出: [ [1,1,2], [1,2,1], [2,1,1] ] class Solut ...

随机推荐

  1. DataFrame的iloc与loc的区别是什么?

    对于一个DataFrame A,A.loc[k]是读取A中index为k的那一行.A.iloc[k]是读取A中的第k行.

  2. LAMP总四部分

    第一部分 1. 安装mysqlcd /usr/local/src/ 免安装编译二进制的包wget http://syslab.comsenz.com/downloads/linux/mysql-5.1 ...

  3. POJ 3249:Test for Job(拓扑排序+DP)

    题意就是给一个有向无环图,每个点都有一个权值,求从入度为0的点到出度为0点路径上经过点(包括起点终点)的权值和的最大值. 分析: 注意3点 1.本题有多组数据 2.可能有点的权值是负数,也就是结果可能 ...

  4. The following signatures couldn't be verified because the public key is not available 解决方法

    今天试图把 deepin 的软件源加到我到 Ubuntu 16.04 中去. 在 deepin wiki 上看到一个教程. 在 /etc/apt/sources.list 中加上 deepin 的软件 ...

  5. Linux(Centos) 搭建ReviewBoard

    一.官方安装手册 reviewboard 的安装用户手册:猛击这里 二.常用安装步骤 2.1.安装httpd,+ mod_wsgi, fastcgi, or mod_python yum -y int ...

  6. 创建 router 连通 subnet

    上一节我们为 Neutron 虚拟路由器配置好了 L3 agent,今天将创建虚拟路由器“router_100_101”,打通 vlan100 和 vlan101. 打开操作菜单 Project -& ...

  7. .net web api ioc unity usage

    1.use nuget to install unity.webapi 2.add configurations in application_start folder using Microsoft ...

  8. poj 3169&hdu3592(差分约束)

    Layout Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9687   Accepted: 4647 Descriptio ...

  9. 过滤器解决hibernate中懒加载问题

    使用过滤器解决懒加载问题需要我们对过滤器的生命周期有深刻的理解 1.浏览器发送一个请求 2.请求通过过滤器执行dofilter之前的代码 3.浏览器通过过滤器到达Servlet(注意我们这里的serv ...

  10. [Machine Learning with Python] My First Data Preprocessing Pipeline with Titanic Dataset

    The Dataset was acquired from https://www.kaggle.com/c/titanic For data preprocessing, I firstly def ...