【数字全排列】LeetCode 47. Permutations II
LeetCode 47. Permutations II
Solution1:我的答案
笨蛋方法:和第46题的思路差不多,把vector换成了set就OK啦~~~
class Solution {
public:vector<vector<int>> permuteUnique(vector<int>& nums) {set<vector<int> > res;vector<vector<int> > my_res;vector<int> temp_res;if(nums.size() == 0)return my_res;else if(nums.size() == 1) {my_res.push_back(nums);return my_res;}int level = 0;vector<bool> visited(nums.size(), false);my_DFS(nums, res, temp_res, visited, level);return vector<vector<int> > (res.begin(), res.end());}void my_DFS(vector<int>& nums, set<vector<int> >& res, vector<int>& temp_res, vector<bool>& visited, int level) {if(level == nums.size()) {res.insert(temp_res);return;}else {for(int i = 0; i < nums.size(); i++) {if(!visited[i]) {temp_res.push_back(nums[i]);visited[i] = true;my_DFS(nums, res, temp_res, visited, level + 1);temp_res.pop_back();visited[i] = false;}}}}
};
Solution2:
参考网址:[1]http://www.cnblogs.com/grandyang/p/4359825.html
[2]https://www.cnblogs.com/reboot329/p/6053808.html
【链接2讲的比较明白】还是DFS,不过有重复,那重点就是去重了。
同样深度的情况下,出现重复的,那么需要跳过。 具体说就是:
判断是否和上一个相等,相等的情况下如果上一个没用过,说明是上一个回溯结束的,同一层,那么就不要再重新来一轮了,跳过。 112 分别以1,1,2开始,第二个1,和第一个1开始的结果重复的。
在第一个1开始的时候,下一层的当前元素是第二个1,虽然也是1,但是上一个1被用了,说明它是不同深度的,所以不跳过。
这道题是之前那道 Permutations 全排列的延伸,由于输入数组有可能出现重复数字,如果按照之前的算法运算,会有重复排列产生,我们要避免重复的产生,在递归函数中要判断前面一个数和当前的数是否相等,如果相等,前面的数必须已经使用了,即对应的visited中的值为1,当前的数字才能使用,否则需要跳过,这样就不会产生重复排列了,代码如下:
class Solution {
public:vector<vector<int> > permuteUnique(vector<int> &num) {vector<vector<int> > res;vector<int> out;vector<int> visited(num.size(), 0);sort(num.begin(), num.end());permuteUniqueDFS(num, 0, visited, out, res);return res;}void permuteUniqueDFS(vector<int> &num, int level, vector<int> &visited, vector<int> &out, vector<vector<int> > &res) {if (level >= num.size()) res.push_back(out);else {for (int i = 0; i < num.size(); ++i) {if (visited[i] == 0) {if (i > 0 && num[i] == num[i - 1] && visited[i - 1] == 0) continue;visited[i] = 1;out.push_back(num[i]);permuteUniqueDFS(num, level + 1, visited, out, res);out.pop_back();visited[i] = 0;}}}}
};
【数字全排列】LeetCode 47. Permutations II相关推荐
- LeetCode 47. Permutations II
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- 47. Permutations II 1
题目: Given a collection of numbers that might contain duplicates, return all possible unique permutat ...
- 47. Permutations II
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- 全排列算法(无重复数字全排列/有重复数字全排列)/ 组合算法/ 求子集算法
写在前面 全排列 1 无重复数字全排列 1.1 紫书版本 1.2 回溯法 2 有重复数字全排列 复盘易错点(可跳过) 写在前面 很久很久以前就想写的一篇博客,因为懒一直没开工,但是学习全排列算法算是我 ...
- [LeetCode] Permutations II 全排列之二
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- 【数字全排列】LeetCode 46. Permutations
LeetCode 46. Permutations Solution0: 补充一个偷鸡摸狗的方法.偷懒的做法直接使用std::next_permutation()函数 class Solution { ...
- 【数字全排列】LeetCode 31. Next Permutation
LeetCode 31. Next Permutation 参考博客:http://www.cnblogs.com/grandyang/p/4428207.html Solution0:一个偷鸡摸狗的 ...
- LeetCode:Permutations, Permutations II(求全排列)
Permutations Given a collection of numbers, return all possible permutations. For example, [1,2,3] ...
- 【暴力枚举】LeetCode 90. Subsets II
LeetCode 90. Subsets II solution1和2均是利用set的,3和4是不利用set的 Solution1:我的答案 迭代法 class Solution { public:v ...
最新文章
- Android LayoutInflater 的使用
- Redis安装整理(window平台) +php扩展redis
- Python之Mysql及SQLAlchemy操作总结
- 企业合理避税三大技巧
- 相关分析在微生物生态学中的应用与误用
- python 使用socks 爬虫_python爬虫基础之urllib的使用
- html的<input type='radio'/>change事件坑
- 【Vue】自定义指令(全局定义 局部定义)
- ubuntu10.04下 简单配置samba
- mac 下php,Mac 下 PHP
- Python_爬虫_BeautifulSoup网页解析库
- php图片滑动代码,基于mootools 1.3框架下的图片滑动效果代码_Mootools
- Jenkins进阶系列之——07Jenkins纳入版本控制
- 深度学习(二十一)基于FCN的图像语义分割-CVPR 2015
- python简明教程_03
- 【机器学习数学基础】Mathematics for Machine Learning 梳理+习题答案
- 解决Tomcat中文乱码问题——windows平台
- install par
- 盘点SQL中最难的5件事
- linux+sftp限制目录,Linux 限制SFTP用户目录和权限
热门文章
- VM安装rhel或linux后,声音很响,如何关闭
- ios开发 方形到圆的动画_使用UIBezierPath画个圆动画
- python安装jupyterlab_JupyterLab的安装及使用
- 计算机课程布置作业,计算机是如何工作的(教案)
- 图片服务器 文件系统,调研分享:图片文件在各文件系统上的访问性能对比
- java 异常处理发生异常_Java中的异常处理
- angularjs绑定属性_AngularJS指令和数据绑定示例教程
- Java中的System.exit()
- 教你前端面试技巧,教你如何涨薪!
- 学习web前端前景怎么样?