题目

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

解答

C语言的实现以前总结过,请见 http://www.cnblogs.com/YuNanlong/p/6171459.html

这道题做的我很气,自己写交换vector元素的过程,只击败了30+%的提交,用了swap()函数击败了70+%的。。。

下面是AC的代码:

class Solution {
public:
    vector<vector<int>> ans;
    vector<int> res;
    void permute(vector<int> nums){
        int length = nums.size();
        if(length == 1){
            res.push_back(nums[0]);
            ans.push_back(vector<int>(res.begin(), res.end()));
            res.pop_back();
            return ;
        }
        else{
            int last;
            for(int i = 0; i < length; i++){
                if(i == 0){
                    last = nums[i];
                }
                else if(last == nums[i]){
                    continue;
                }
                last = nums[i];
                swap(nums[0], nums[i]);
                res.push_back(last);
                permute(vector<int>(nums.begin() + 1, nums.end()));
                res.pop_back();
            }
        }
    }
    vector<vector<int>> permuteUnique(vector<int>& nums) {
        sort(nums.begin(), nums.end());
        permute(vector<int>(nums.begin(), nums.end()));
        return ans;
    }
};

107

LeetCode OJ 47. Permutations II的更多相关文章

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

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

  2. 【一天一道LeetCode】#47. Permutations II

    一天一道LeetCode系列 (一)题目 Given a collection of numbers that might contain duplicates, return all possibl ...

  3. 【LeetCode】47. Permutations II

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

  4. 【LeetCode】47. Permutations II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:递归 方法二:回溯法 日期 题目地址:htt ...

  5. LeetCode 【47. Permutations II】

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

  6. LeetCode OJ:Permutations II(排列II)

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

  7. [LeetCode] 47. Permutations II 全排列之二

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

  8. leetCode 47.Permutations II (排列组合II) 解题思路和方法

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

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

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

随机推荐

  1. ArcMap VBA实现连续编号

    连续编号VBA部分:Static lCount as longlCount=lCount+1赋值部分:lCount (从1开始)lCount-1 (从0开始)

  2. centos7 使用二进制安装mysql 5.7.23

    1.下载二进制安装包 mysql-5.7.23-linux-glibc2.12-x86_64.tar.gz cd /usr/local/src wget https://cdn.mysql.com// ...

  3. 矩阵半正定: positive semidefinite

    具体定义:https://en.wikipedia.org/wiki/Positive-definite_matrix

  4. Angular2 入门详解

    AngularJS 2 快速入门 npm是什么?   npm其实是Node.js Package Manager的简称,是Node.js包管理工具(package manager) 安装Node.js ...

  5. (转)Linux 系统服务的启动顺序解析 rc.*

    介绍系统按照不同级别启动时需要启动的服务. 进入目录:etc 执行命令:ls -l | grep "rc.*" | sort 结果如下图:   1 系统在启动时,通过inittab ...

  6. Python绘制2D图像

    封装了一个简单的2d绘图函数 from matplotlib import pyplot as plt def plot_line(*args, **kw): """ : ...

  7. linux 安装scala

    1. 下载scala 到scala官网下载scala https://www.scala-lang.org/download/,目前最新版本是2.12.8 wget https://downloads ...

  8. vue使用animate.css库

    //引入库 <link rel="stylesheet" type="text/css" href="animate.css"> ...

  9. Jscraft 使用 Shell 与预先加载别名混合使用

    Session session = a.getSessionShell("user", "pwd", "host"); Channel ch ...

  10. 最简单的DHCP服务

    这几天在准备网络安装linux操作系统.最后决定用pxe + kickstart 的方式完成.原理.方案弄完了之后,开始搭建,结果被DHCP给挡住了.这不就得研究研究最简单最实用的DHCP使用方法. ...