leetcode47. Permutations II

题意:

给定可能包含重复的数字的集合,返回所有可能的唯一排列。

思路:

这题全排列两种写法。

  • 用hash表记录已经visit的数字,虽然看起来很朴实,但是比另一种方法稳多了,而且hash表的查找也是很高效的。
  • 另一种用swap进行交换,确保已经遍历的点在前面,未遍历的点在后面。这个方法在没有duplicate的情况下,比如上一题,看不出有什么坑点。有了duplicate之后感觉很鸡儿皮。 首先要用sort排序,不然的话还是会遍历到重复的点的。每次寻找下一个点的时候还要用sort排一次序,因为每次经过排序之后,用两个swap已经无法还原了,一定要再次排序。 用swap的话特别注意[0,0,0,1,9]这个样例,让我感觉swap坑点有点fly。

ac代码:

swap方法

C++

class Solution {
public:
vector<vector<int>>res;
vector<vector<int>> permuteUnique(vector<int>& nums) {
res.clear();
vector<int> temp; dfs(nums,temp,0);
return res;
} void dfs(vector<int>& nums,vector<int> temp, int begin)
{
if(begin >= nums.size())
{
res.push_back(temp);
return;
}
sort(nums.begin() + begin,nums.end());
for(int i = begin; i < nums.size(); i++)
{
temp.push_back(nums[i]);
swap(nums[i], nums[begin]);
dfs(nums,temp,begin + 1);
temp.pop_back();
//swap(nums[i], nums[begin]);
sort(nums.begin() + begin,nums.end());
while(i + 1 < nums.size() && nums[i + 1] == nums[i]) i++;
}
} };

python


leetcode47. Permutations II的更多相关文章

  1. LeetCode47.Permutations II(剑指offer38-1)

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

  2. Leetcode47. Permutations II全排列2

    给定一个可包含重复数字的序列,返回所有不重复的全排列. 示例: 输入: [1,1,2] 输出: [ [1,1,2], [1,2,1], [2,1,1] ] 在全排列1题目的基础上先排序,目的是把相同的 ...

  3. LeetCode46,47 Permutations, Permutations II

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

  4. 【leetcode】Permutations II

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

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

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

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

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

  7. leetcode Permutations II 无重全排列

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

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

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

  9. Permutations,Permutations II,Combinations

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

随机推荐

  1. linux网络配置完全解析

    概述:熟悉了windows下面的网络配置,对linux下的网络配置缺未必了解透彻.熟练掌握linux下的网络配置原理,能帮助我们更容易掌握网络传输原理:同时具备一些网络连接不通对应问题的排查能力.文本 ...

  2. php pdo封装类

    class MYPDO { protected static $_instance = null; protected $dbname = ''; protected $dsn; protected ...

  3. 用OpenSSL命令行生成证书文件

    用OpenSSL命令行生成证书文件 1.首先要生成服务器端的私钥(key文件): openssl genrsa -des3 -out server.key 1024 运行时会提示输入密码,此密码用于加 ...

  4. [你必须知道的.NET]第二十一回:认识全面的null

    发布日期:2008.7.31 作者:Anytao © 2008 Anytao.com ,Anytao原创作品,转贴请注明作者和出处. 说在,开篇之前 null.nullable.??运算符.null ...

  5. IEEEXtreme 10.0 - Game of Stones

    这是 meelo 原创的 IEEEXtreme极限编程大赛题解 Xtreme 10.0 - Game of Stones 题目来源 第10届IEEE极限编程大赛 https://www.hackerr ...

  6. [实战]MVC5+EF6+MySql企业网盘实战(7)——文件上传

    写在前面 周末了,在家继续折腾网盘,今天实现网盘文件的上传. 系列文章 [EF]vs15+ef6+mysql code first方式 [实战]MVC5+EF6+MySql企业网盘实战(1) [实战] ...

  7. 第三方登陆微博、qq、微信

    源文:http://blog.csdn.net/tivonalh/article/details/60954373 假设是已经申请完成各平台开发者账号. 先来简单的,微博和QQ 微博: 引入微博JS ...

  8. 【LOJ】#2007. 「SCOI2015」国旗计划

    题解 考虑朴素的做法,断环为链,复制2M个,找到一个位置i,f(i)是这个位置之前开始的线段,结束位置最远的位置在哪 然后对于每一个人,从自己线段的起点往下跳,跳到起点+M或以后的步数就是答案 我们发 ...

  9. linux中使用rm命令将文件移到回收站的方法

    今天在终端下,看到我的用户目录下有个-的文件夹(maven生成),相要删除收回点空间,习惯性的用命令 rm -rf ~ ,一回车,猛然想起的时候已经来不及了,世界一下子清静了,想死的心都有了! 没错, ...

  10. XV6操作系统代码阅读心得(四):虚拟内存

    本文将会详细介绍Xv6操作系统中虚拟内存的初始化过程. 基本概念 32位X86体系结构采用二级页表来管理虚拟内存.之所以使用二级页表, 是为了节省页表所占用的内存,因为没有内存映射的二级页表可以不用分 ...