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:是否允许重复组合 ...
随机推荐
- skb_reserve(skb,2)中的2的意义
skb_reserve() skb_reserve()在数据缓存区头部预留一定的空间,通常被用来在数据缓存区中插入协议首部或者在某个边界上对齐.它并没有把数据移出或移入数据缓存区,而只是简单地更新了数 ...
- openjudge-NOI 2.6-1768 最大子矩阵
题目链接:http://noi.openjudge.cn/ch0206/1768/ 题解: 如果用O(n4)的算法肯定会炸,需要压缩掉一维的空间,只需要简单加和就好啦 例如,我们要对样例中第2-4行D ...
- 工作当中遇到的ssh错误
[root@1bcc1d3f9666 externalscripts]# /usr/sbin/sshd Could not load host key: /etc/ssh/ssh_host_rsa_k ...
- ASP.NET Core 2.0 MVC 发布部署--------- SUSE 16 Linux Enterprise Server 12 SP2 X64 具体操作
.Net Core 部署到 SUSE 16 Linux Enterprise Server 12 SP2 64 位中的步骤 1.安装工具 1.apache 2..Net Core(dotnet-sdk ...
- Spring Boot Admin Quick Start
Quick Start 1. Spring Boot Admin是干什么的? 用来监控Spring Boot项目,可以通过Spring Boot Admin Client(via Http) 或者 使 ...
- csu 1806 & csu 1742 (simpson公式+最短路)
1806: Toll Time Limit: 5 Sec Memory Limit: 128 MB Special JudgeSubmit: 256 Solved: 74[Submit][Sta ...
- EasyUi – 6.easyui常见问题
1.进度条 2.JQuery EasyUI弹出对话框解决Asp.net服务器控件无法执行后台代码的方法 3. 三张表的连接查询现在到datagrid里 4.日期组合框DateBox设置readonly ...
- 安迪的第一个字典(UVa10815)
题目具体描述见:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_prob ...
- java EE :GenericServlet 抽象类、ServletConfig 接口
ServletConfig 接口:当前 Servlet 在 web.xml 中相关配置信息 package javax.servlet; import java.util.Enumeration; p ...
- DotNetOpenAuth实践之Webform资源服务器配置
系列目录: DotNetOpenAuth实践系列(源码在这里) 上篇我们讲到WebApi资源服务器配置,这篇我们说一下Webform下的ashx,aspx做的接口如何使用OAuth2认证 一.环境搭建 ...