题目

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. taskkill帮助信息

    taskkill帮助信息: C:\Users\xusweeter>taskkill /? TASKKILL [/S system [/U username [/P [password]]]] { ...

  2. 员工管理系统(集合与IO流的结合使用 beta1.0 ArrayList<Employee>)

    package cn.employee; public class Employee { private int empNo; private String name; private String ...

  3. Kali linux 2016.2(Rolling)里的枚举服务

    前言 枚举是一类程序,它允许用户从一个网络中收集某一类的所有相关服务.

  4. 来自AJPFX的二分法查找

    package com.heima.array; public class Demo2_Array { /**         * * A:案例演示                        * ...

  5. Caused by: javax.el.PropertyNotFoundException: Property 'product' not found on type java.lang.String

    今天在JSP利用EL表达式取值报了 "javax.el.PropertyNotFoundException”,经过debug和打印将问题定位到这段代码: HTML应该是没啥问题,看提示在ja ...

  6. JAVA300集笔记

    章节2 java入门阶段 2.1注释 单行注释 //  多行注释 /* 内容*/ 文本注释/**内容*/ 注释是为了方便阅读代码,在编译时注释会被删除. 2.2 标识符 标识符作用: 标识符用来给变量 ...

  7. js插件定义事件中,this引用的实现模拟

    在web项目中,经常会使用jquery和mui等js框架,之前只是按照API说明去按规则使用,比如在jq和mui中,事件处理函数中可以直接用this访问事件源,如下面的代码: <!DOCTYPE ...

  8. R in action读书笔记(10)-第八章:回归-- 异常观测值 改进措施

    8.4 异常观测值 8.4.1 离群点 car包也提供了一种离群点的统计检验方法.outlierTest()函数可以求得最大标准化残差绝对值Bonferroni调整后的p值: > library ...

  9. SQL Server 零散笔记

    排序显示行号 select Row_Number() over(order by Code) as RowNumber,ID,Code,Name from CBO_ItemMaster 不排序显示行号 ...

  10. iTOP-6818开发板设置NFS共享目录的实现

    NFS 共享目录的制作过程.主要分为两个步骤:1.搭建 NFS 服务器2.配置内核. NFS 是 Network FileSystem 的缩写,是由 SUN 公司研制的 UNIX 表示层协议(pres ...