leetcode47. Permutations II
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的更多相关文章
- LeetCode47.Permutations II(剑指offer38-1)
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- Leetcode47. Permutations II全排列2
给定一个可包含重复数字的序列,返回所有不重复的全排列. 示例: 输入: [1,1,2] 输出: [ [1,1,2], [1,2,1], [2,1,1] ] 在全排列1题目的基础上先排序,目的是把相同的 ...
- LeetCode46,47 Permutations, Permutations II
题目: LeetCode46 I Given a collection of distinct numbers, return all possible permutations. (Medium) ...
- 【leetcode】Permutations II
Permutations II Given a collection of numbers that might contain duplicates, return all possible uni ...
- LeetCode:Permutations, Permutations II(求全排列)
Permutations Given a collection of numbers, return all possible permutations. For example, [1,2,3] h ...
- leetcode总结:permutations, permutations II, next permutation, permutation sequence
Next Permutation: Implement next permutation, which rearranges numbers into the lexicographically ne ...
- leetcode Permutations II 无重全排列
作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4051169.html 题目链接:leetcode Permutations II 无重全排 ...
- [Leetcode][Python]47: Permutations II
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 47: Permutations IIhttps://oj.leetcode. ...
- Permutations,Permutations II,Combinations
这是使用DFS来解数组类题的典型题目,像求子集,和为sum的k个数也是一个类型 解题步骤: 1:有哪些起点,例如,数组中的每个元素都有可能作为起点,那么用个for循环就可以了. 2:是否允许重复组合 ...
随机推荐
- 关于parse_str变量覆盖分析
这个漏洞有两个姿势.一个是不存在的时候一个是存在的时候. 经过测试该漏洞只在php5.2中存在,其余均不存在. 倘若在parse_str函数使用的代码上方未将其定义那么即存在变量覆盖漏洞否则不行. 还 ...
- 64_r1
R-3.4.0-2.fc26.x86_64.rpm 15-May-2017 14:49 31030 R-ALL-1.6.0-4.fc26.noarch.rpm 17-Feb-2017 22:05 11 ...
- 【UOJ#38】【清华集训2014】奇数国
考虑欧拉函数的性质,60很小,压位存下线段树每个节点出现质数. #include<bits/stdc++.h> ; ; typedef long long ll; using namesp ...
- php7.33 configure
To assign environment variables (e.g., CC, CFLAGS...), specify them as VAR=VALUE. See below for desc ...
- 报错:/application/zabbix/sbin/zabbix_server: error while loading shared libraries: libmysqlclient.so.18: cannot open shared object file: No such file or directory
启动zabbix_server时报错: /application/zabbix/sbin/zabbix_server: error while loading shared libraries: li ...
- 深度学习方法(七):最新SqueezeNet 模型详解,CNN模型参数降低50倍,压缩461倍!
欢迎转载,转载请注明:本文出自Bin的专栏blog.csdn.net/xbinworld. 技术交流QQ群:433250724,欢迎对算法.技术感兴趣的同学加入. 继续前面关于深度学习CNN经典模型的 ...
- 洛谷 P1652圆 题解
题目传送门 这道题也就是考你对几何的了解: 圆与圆没有公共点且一个圆在另一个圆外面时,叫做圆与圆相离. 当圆心距大于两圆半径之和时,称为两圆外离: 当圆心距小于两圆半径之差的绝对值时,称为两圆内含. ...
- EasyUi – 2.布局Layout + 3.登录界面
1.页面布局 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="index.a ...
- Web APi入门之Self-Host(二)
这篇来讲讲WebApi的自托管,WebApi可以托管到控制台/winform/服务上,并不是一定要依赖IIS才行. 1.首先新建控制台项目,在通过Nuget搜索Microsoft.AspNet.Web ...
- [实战]MVC5+EF6+MySql企业网盘实战(14)——逻辑重构
写在前面 上篇文章关于修改文件夹和文件名称导致的找不到物理文件的问题,这篇文章将对其进行逻辑的修改. 系列文章 [EF]vs15+ef6+mysql code first方式 [实战]MVC5+EF6 ...