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]
]

分析: 全组合的思想,保证start和end之间交换的时候中间没有与end相同的数字

class Solution {
public:
void swap(int i, int j, vector<int>& nums){
int temp =nums[i];
nums[i] = nums[j];
nums[j]= temp;
}
bool isSwap(int start, int end, vector<int>& nums){
for(; start<end; start++)
if(nums[start] == nums[end])
return false;
return true;
} void allRange(int start, vector<int>& nums, vector<vector<int>>& res)
{
if(start==nums.size()-1)
return;
for(int i =start; i<nums.size(); i++){
if(isSwap(start,i,nums))
{
swap(start, i, nums);
if(start!=i)
res.push_back(nums);
allRange(start+1, nums, res);
swap(i, start, nums);
} }
}
vector<vector<int>> permuteUnique(vector<int>& nums) {
vector<vector<int>> res;
if(nums.size()==0)
return res;
res.push_back(nums);
allRange(0, nums, res);
return res;
}
};

  

Permutations II的更多相关文章

  1. 【leetcode】Permutations II

    Permutations II Given a collection of numbers that might contain duplicates, return all possible uni ...

  2. LeetCode:Permutations, Permutations II(求全排列)

    Permutations Given a collection of numbers, return all possible permutations. For example, [1,2,3] h ...

  3. leetcode总结:permutations, permutations II, next permutation, permutation sequence

    Next Permutation: Implement next permutation, which rearranges numbers into the lexicographically ne ...

  4. LeetCode46,47 Permutations, Permutations II

    题目: LeetCode46 I Given a collection of distinct numbers, return all possible permutations. (Medium) ...

  5. leetcode Permutations II 无重全排列

    作者:jostree  转载请注明出处 http://www.cnblogs.com/jostree/p/4051169.html 题目链接:leetcode Permutations II 无重全排 ...

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

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

  7. Permutations,Permutations II,Combinations

    这是使用DFS来解数组类题的典型题目,像求子集,和为sum的k个数也是一个类型 解题步骤: 1:有哪些起点,例如,数组中的每个元素都有可能作为起点,那么用个for循环就可以了. 2:是否允许重复组合 ...

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

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

  9. Permutations II - LeetCode

    目录 题目链接 注意点 解法 小结 题目链接 Permutations II - LeetCode 注意点 不确定有几种排列 解法 解法一:因为有重复的数字所以排列的个数不确定几个,一直生成新的排列直 ...

  10. 【LeetCode】47. Permutations II

    Permutations II Given a collection of numbers that might contain duplicates, return all possible uni ...

随机推荐

  1. geotrellis使用(二十四)将Geotrellis移植到CDH中必须要填的若干个坑

    目录 前言 若干坑 总结 一.前言        近期干了一件事情,将geotrellis程序移植到CDH中(关于CDH,可以参考安装ClouderaManager以及使用ClouderaManage ...

  2. ZFPlayer 源码解读

    源码下载地址:https://github.com/renzifeng/ZFPlayer 之前自己实现过一个模仿百思不得姐的demo https://github.com/agelessman/FFm ...

  3. 高仿QQ顶部控件之IOS SegmentView

    经常会看到QQ上面有一个 消息和电话 的顶部面板,这个空间是IOS7的分段控制,android中没有这个控件,今天在威哥的微信公众号中成功gank到这个自定义控件的实现,下面跟着尝试一波. 首先是定义 ...

  4. FFmpeg数据结构:AVPacket解析

    本文主要从以下几个方面对AVPacket做解析: AVPacket在FFmpeg中的作用 字段说明 AVPacket中的内存管理 AVPacket相关函数的说明 结合AVPacket队列说明下AVPa ...

  5. TYPESDK手游聚合SDK客户端远程开关:渠道支付黑名单

    渠道支付要做开关干嘛用呢?为什么要做这种东西呢? 这个教训来分享一下,我们的游戏上线公测了,59个渠道首发,其中包括了应用宝,UC,360等的大渠道,也包含有一些工会渠道和小渠道,上线后一切正常,但是 ...

  6. css单行文本与多行溢出文本的省略号问题

    在文字布局和代码编写过程中遇到文本溢出是常有的事,下面总结一下对于单行文本溢出和多行文本溢出省略号的处理. 一.单行文本省略号 <p class="text1"> 这是 ...

  7. JSONP实现

    使用jsonp实现跨域获取数据. js部分(旧): (function(window, document) { 'use strict'; var jsonp = function(url, data ...

  8. Web Mercator Non-Conformal, Non-Mercator

    public static void XYtoGL(Coordinate coordinate) { double R = 6378137; coordinate.x = coordinate.x / ...

  9. 【代码笔记】iOS-获得当前硬盘空间

    一,代码. - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. ...

  10. CentOS 7 安装 Docker

    CentOS 7 安装 Docker 这里介绍 ContOS 7 的安装 docker V1.2+,包括阿里云加速 docker 镜像下载的设置,这对提升使用 docker 体验至关重要.其他系统安装 ...