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. flask基础之jijia2模板语言进阶(三)

    前言 前面学习了jijia2模板语言的一些基础知识,接下来继续深挖jijia2语言的用法. 系列文章 flask基础之安装和使用入门(一) flask基础之jijia2模板使用基础(二) 控制语句 和 ...

  2. 在64位ubuntu中安装代码比较工具beyond compare

    1,//从http://www.scootersoftware.com/download.php 官方地址下载 bcompare-3.3.2.14050.tar.gz 或 bcompare-4.0.7 ...

  3. python 之ConfigParser模块学习

    1.1 读取配置文件 -read(filename) 直接读取ini文件内容 -sections() 得到所有的section,并以列表的形式返回 -options(section) 得到该secti ...

  4. 使用xbee连接地面站和飞控

    Zigbee是一种短距离.低功耗的近距离无线组网通讯技术,主要适用于自动控制和远程控制领域,可以嵌入各种设备. DIGI的ZigBee产品XBee小型但却是一个功能完善的ZigBee收发器(即接收器/ ...

  5. Java访问http用户验证

    public class SAXParse { static final String kuser = "admin"; static final String kpass = & ...

  6. 一致性hash理解

    在做memcached分布式集群时往往要用到一致性hash算法来调节缓存数据的分布. 通常的hash算法是以服务器数量N作为模数,使用key%N的值来获得最终位置,显然当服务器数量发生变化即N发生变化 ...

  7. 苹果容器超出内容overflow滑动卡顿问题

    -webkit-overflow-scrolling:touch; 就这么一段代码,加载需要滚动的容器css样式中.因为苹果的硬件加速产生的后果....

  8. Linux打补丁的一个简单例子

        前言 在做开发的过程中难免需要给内核及下载的一些源码打补丁或者说是升级,所以我们学习在Linux下使用diff制作补丁以及如何使用patch打补丁显得尤为重要. diff与patch命令介绍 ...

  9. golang基础之三-字符串,时间,流程控制,函数

    strings和strconv的使用 strings strings.HasPrefix(s string,preffix string) bool:判断字符串s是否以prefix开头 stirngs ...

  10. git配置用户名跟邮箱

    因为我有两个git账号 所以我现在要改变我的默认用户名跟邮件 我就需要去终端设置用户名跟邮箱 具体的命令行就是 设置git的用户名 git config --global user.name &quo ...