Leetcode47. Permutations II全排列2
给定一个可包含重复数字的序列,返回所有不重复的全排列。
示例:
输入: [1,1,2] 输出: [ [1,1,2], [1,2,1], [2,1,1] ]
在全排列1题目的基础上先排序,目的是把相同的元素聚在一起,然后在递归完一个数时,把后面与他相同的数给过滤掉,防止重复
class Solution {
public:
vector<vector<int> >res;
vector<int> visit;
int size;
vector<vector<int> > permuteUnique(vector<int>& nums)
{
int len = nums.size();
if(len == 0)
return res;
sort(nums.begin(), nums.end());
size = len;
visit = vector<int>(len, 0);
vector<int> temp;
DFS(nums, temp, 0);
return res;
}
void DFS(vector<int>& nums, vector<int> &temp, int len)
{
if(len == size)
{
res.push_back(temp);
}
for(int i = 0; i < size; i++)
{
if(visit[i] == 1)
continue;
visit[i] = 1;
temp.push_back(nums[i]);
DFS(nums, temp, len + 1);
visit[i] = 0;
temp.pop_back();
for(; i < size - 1; i++)
{
if(nums[i] == nums[i + 1])
continue;
else
break;
}
}
}
};
Leetcode47. Permutations II全排列2的更多相关文章
- leetcode47. Permutations II
leetcode47. Permutations II 题意: 给定可能包含重复的数字的集合,返回所有可能的唯一排列. 思路: 这题全排列两种写法. 用hash表记录已经visit的数字,虽然看起来很 ...
- [LeetCode] Permutations II 全排列之二
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- [LeetCode] 47. Permutations II 全排列之二
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- [Leetcode] permutations ii 全排列
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- LeetCode47.Permutations II(剑指offer38-1)
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- [LeetCode] 47. Permutations II 全排列 II
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- permutations II(全排列 2)
题目要求 Given a collection of numbers that might contain duplicates, return all possible unique permuta ...
- 47. Permutations II (全排列有重复的元素)
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- leetcode Permutations II 无重全排列
作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4051169.html 题目链接:leetcode Permutations II 无重全排 ...
随机推荐
- MySQL:MySQL 存储过程
ylbtech-MySQL:MySQL 存储过程 1.返回顶部 1. MySQL 存储过程 MySQL 5.0 版本开始支持存储过程. 存储过程(Stored Procedure)是一种在数据库中存储 ...
- System.Web.Mvc.ViewResultBase.cs
ylbtech-System.Web.Mvc.ViewResultBase.cs 1.程序集 System.Web.Mvc, Version=5.2.3.0, Culture=neutral, Pub ...
- Android之相关术语
Dalvik: Android特有的虚拟机,和JVM不同,Dalvik虚拟机非常适合在移动终端上使用! AVD: (android virtual machine):安卓虚拟设备,就是安卓的模拟器 A ...
- Python ----键抠图
背景 这段时间,经常有人来找我,说我是学计算机的,能不能帮他p一下证件照,我只想说,MMP的,我是学计算机的不错,可我不会ps阿. 我想了一会,python 这么火,能不能来个自动抠图,说好就干吧 介 ...
- java基础之Character类概述
Character 类 在对象中包装一个基本类型 char 的值 此外,该类提供了几种方法,以确定字符的类别(小写字母,数字,等等),并将字符从大写转换成小写,反之亦然 构造方法 public Cha ...
- Activiti 变量设置
使用工作流的时候必定会附上一些变量.例如,请假的时候有填写请假理由,天数等等.可以用以下代码实现 public void setVariables(){ /**与任务(正在执行)*/ TaskServ ...
- 网络结构解读之inception系列一:Network in Network
网络结构解读之inception系列一:Network in Network 网上有很多的网络结构解读,之前也是看他人博客的介绍,但当自己看论文的时候,发现存在很多的细节和动机解读,而这部分能加深 ...
- SyntaxError: Non-ASCII character ‘xe5’ in file 04.py on line 4, but no encoding declared
出现问题的原因:程序中的编码错误,python默认是acii模式,没有支持utf8,代码中需要输出汉字,所以报错. 解决办法:源代码文件第一行添加:#coding:utf-8 -- coding: U ...
- day49作业
结合前端,django,MySQL,pymysql模块实现数据库数据动态展示到前端 效果图: 数据交互流程 urls.py代码: from django.conf.urls import url fr ...
- pip更新升级后Import Error:cannot import name main及pip安装包后出现环境错误拒绝访问
1. sudo gedit /usr/bin/pip 将pip改为pip._internal 2.pip install XX 改为 pip install --user XX