Leetcode Permutations II
来源:互联网 发布:红米note3数据经常断网 编辑:程序博客网 时间:2024/05/23 11:28
Given a collection of numbers that might contain duplicates, return all possible unique permutations.
For example,[1,1,2]
have the following unique permutations:[1,1,2]
, [1,2,1]
, and [2,1,1]
.
Difficulty: Medium
回溯的基础上加一行判断。
</pre><pre name="code" class="cpp">class Solution {public: void back_tracking(vector<vector<int> > &ans, vector<int> &temp, vector<int> &nums, vector<int> &flag){ if(temp.size() == nums.size()){ ans.push_back(temp); return; } for(int i = 0; i < flag.size(); i++){ if(flag[i] == 1 || (i > 0 && nums[i] == nums[i-1] && flag[i-1] == 0)) continue; temp.push_back(nums[i]); flag[i] = 1; back_tracking(ans, temp, nums, flag); flag[i] = 0; temp.pop_back(); } return; } vector<vector<int>> permuteUnique(vector<int>& nums) { sort(nums.begin(), nums.end()); vector<vector<int> > ans; vector<int> temp; vector<int> flag(nums.size(), 0); back_tracking(ans, temp, nums, flag); return ans; }};
0 0
- 【LeetCode】Permutations && Permutations II
- leetcode: Permutations/Permutations II
- leetcode Permutations & Permutations II
- [Leetcode]Permutations && Permutations II
- Leetcode: Permutations and Permutations II
- leetcode-permutations and permutations II
- Leetcode:Permutations与Permutations II
- LeetCode: Permutations II
- LeetCode Permutations II
- LeetCode: Permutations II
- [Leetcode] Permutations II
- [LeetCode] Permutations II
- [Leetcode] Permutations II
- leetcode Permutations II
- [LeetCode]Permutations II
- leetcode - Permutations II
- LeetCode-Permutations II
- [leetcode] Permutations II
- Use the endorsed directory mechanism to place jaxb-api.jar(jaxb-api.jar、jaxws-api.jar与jdk1.6不兼容问题)
- DrawMenus.cs
- 怎么code review?
- DrawWidget.cs
- java中continue是什么意思?
- Leetcode Permutations II
- SQL Server管理及开发工具(Navicat for SQL Server)v11.2.13
- deploy 自己编写的 QT 程序 (window下打包程序)
- Navicat设定mysql定时任务
- JZOJ 4219 【五校联考2day1】池塘
- 基于表单数据的封装,泛型,反射以及使用BeanUtils进行处理
- 谈谈对攻读计算机研究生的看法
- Doc12(innerText与innerHTML及兼容性写法)
- RFS( 二 )----一些常用的快捷键