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. zookeeper 下载安装

    下载:wget https://www-us.apache.org/dist/zookeeper/zookeeper-3.4.13/zookeeper-3.4.13.tar.gz 解压:tar -zx ...

  2. Mybatis通过接口的方式实现增删改查

    导入jar包 [mybatis] [oracle] 生成数据库 1.添加Mybatis的配置文件mybatis-config.xml 在src目录下创建一个mybatis-config.xml文件,如 ...

  3. 【距离GDOI:141天】 滚入数位DP的坑

    作为博客园的第一篇...我都不知道要写什么了 ... 其实今天很没状态,就当吐槽吧... 嗯,被黄神带去写treap+可持久化线段树,然后在可持久化的删除上面跪了两天,真的是一跪不起.我已经连续多久没 ...

  4. [SDOI2016][bzoj4514] 数字配对 [费用流]

    题面 传送门 思路 一个数字能且只能匹配一次 这引导我们思考:一次代表什么?代表用到一定上限(b数组)就不能再用,同时每用一次会产生价值(c数组) 上限?价值?网络流! 把一次匹配设为一点流量,那产生 ...

  5. HDU 5253 最小生成树 kruscal

    Description 老 Jack 有一片农田,以往几年都是靠天吃饭的.但是今年老天格外的不开眼,大旱.所以老 Jack 决定用管道将他的所有相邻的农田全部都串联起来,这样他就可以从远处引水过来进行 ...

  6. UVA 1619/POJ2796 滑窗算法/维护一个单调栈

    Feel Good Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 12409   Accepted: 3484 Case T ...

  7. 基于SSM3框架FreeMarker自定义指令(标签)实现

    通过之前的Spring MVC 3.0.5+Spring 3.0.5+MyBatis3.0.4全注解实例详解系列文章,我们已经成功的整合到了一起,这次大象将在此基础上对框架中的FreeMarker模板 ...

  8. 基于webpack搭建vue-cli以及webstorm的设置

    N1.检查本地电脑是否安装node和npm 安装vue-cli的前提是已经安装了npm 是否安装npm检查命令 npm -v 如果出现版本号,说明已经安装了npm 查看node版本号 node -v ...

  9. iOS工程中一天只让进行一次的操作如何做?

     转至: iosNSDateNSObject一天一次   整体思路:当进行操作的时候记录操作时间存在偏好设置当中,当再次点击的时候获取现在的时间然后和之前记录的时间进行比较.如果是一天那么就提示“今天 ...

  10. POJ3983 快算24

    很正常的题目,迷一样的答案. 测试数据只有一组,对没错只有一组. #include<cstdio> int main() { printf("5*(5-(1/5))\n" ...