题目

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

分析

用上一题的代码,完全可以AC,那是因为我们的库函数next_permutation()以及prev_permutation()内置排重的代码。。。

其实,题目考察的实质,是让我们自己实现全排,只不过我偷懒了,直接调用了算法库。。。

AC代码

class Solution {
public:
vector<vector<int>> permuteUnique(vector<int>& nums) {
vector<vector<int> > ret; if (nums.empty())
return ret; sort(nums.begin(), nums.end());
ret.push_back(nums);
while (next_permutation(nums.begin(), nums.end()))
ret.push_back(nums); return ret;
}
};

GitHub测试程序源码

LeetCode(47)Permutations II的更多相关文章

  1. LeetCode(275)H-Index II

    题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimi ...

  2. LeetCode(47):全排列 II

    Medium! 题目描述: 给定一个可包含重复数字的序列,返回所有不重复的全排列. 示例: 输入: [1,1,2] 输出: [ [1,1,2], [1,2,1], [2,1,1] ] 解题思路: 这道 ...

  3. LeetCode(52) N-Queens II

    题目 Follow up for N-Queens problem. Now, instead outputting board configurations, return the total nu ...

  4. LeetCode(46)Permutations

    题目 Given a collection of numbers, return all possible permutations. For example, [1,2,3] have the fo ...

  5. Leetcode(213)-打家劫舍II

    你是一个专业的小偷,计划偷窃沿街的房屋,每间房内都藏有一定的现金.这个地方所有的房屋都围成一圈,这意味着第一个房屋和最后一个房屋是紧挨着的.同时,相邻的房屋装有相互连通的防盗系统,如果两间相邻的房屋在 ...

  6. LeetCode(47)-Reverse Bits

    题目: Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented ...

  7. LeetCode(90) Subsets II

    题目 Given a collection of integers that might contain duplicates, nums, return all possible subsets. ...

  8. LeetCode(122) Best Time to Buy and Sell Stock II

    题目 Say you have an array for which the ith element is the price of a given stock on day i. Design an ...

  9. LeetCode(154) Find Minimum in Rotated Sorted Array II

    题目 Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? W ...

随机推荐

  1. Tech 助力Fin ,大数据风控系统赋能掌众金服!

    胡亚海 首席技术官  CTO 北京航空航天大学  博士 深耕互联网领域近20年,先后任职于普天信息技术研究院.摩托罗拉.宇龙酷派.百度等知名企业,曾主导宇龙酷派公司全员从WinCE向Android转型 ...

  2. MYSQL 配置远程连接

    例如,你想myuser使用mypassword从任何主机连接到mysql服务器的话.  GRANT ALL PRIVILEGES ON *.* TO 'myuser'@'%'IDENTIFIED BY ...

  3. Styles and Themens(1)详述

    Styles and Themes IN THIS DOCUMENT Defining Styles Inheritance Style Properties Applying Styles and ...

  4. Snort里的规则目录文件解读(图文详解)

    不多说,直接上干货! snort的规则啊,是基于文本的,它通常存在于snort程序目录中或者子目录中,规则文件按照不同的组,进行分类存放的. snort的安装目录 [root@datatest sno ...

  5. Oracle的一些名词和概念

    1.数据库 这里的数据库不是通常情况下我们所说的数据库,而是一个Oracle的专业名词.它是磁盘上存储数据的集合,在物理上表现为数据文件. 日志文件和控制文件等,在逻辑上以表空间形式存在.使用时,必须 ...

  6. asp.net网站接入QQ登录

    这两天在做网站第三方登录,总结一下QQ登录吧,支付宝就不用了(下载dome把ID什么的换一换就基本可以了.),本文主要说的是代码的实现方式,逻辑部分主要还是根据帮助文档来的.不懂的同学可以先看看文档. ...

  7. 把json数据转化成对象

    把json数据转化到一个对象中,再用对象直接调用 package com.lxj.register; import java.io.BufferedReader; import java.io.IOE ...

  8. Java线程-线程、程序、进程的基本概念

    线程 与进程相似,但线程是一个比进程更小的执行单位.一个进程在其执行的过程中可以产生多个线程. 与进程不同的是同类的多个线程共享同一块内存空间和一组系统资源,所以系统在产生一个线程,或是在各个线程之间 ...

  9. Fragment中获取Activity的Context (转)

    Fragment中获取Activity的Context时只需要this.getActivity()即可.     而不是许多人说的this.getActivity().getApplicationCo ...

  10. C++模版完全解析

    模版 模版在C++中是一个很重要的概练,生活中的模版也是随处可见,比如制造工程师会 哪一个模子去构造出一个一个的工件,C++程序员能够用模版去实例化y有相同操作 不同类型的函数或者数据结构.简单的理解 ...