Permutations II
来源:互联网 发布:淘宝虚拟网店不让开了 编辑:程序博客网 时间:2024/06/05 04:22
在LeetCode(46) Permutations的基础上删除重复的可行解。
仅仅添加了
if(i >= 1 && nums[i] == nums[i - 1]) continue;
完整代码如下:
class Solution {public: void dfs(vector<int> nums, vector<int> &oneResult, int depth, int &length, vector<vector<int>> &result) { if(depth == length - 1) { oneResult[depth] = nums[0]; result.push_back(oneResult); return; } for(int i = 0; i < nums.size(); i++) { if(i >= 1 && nums[i] == nums[i - 1]) continue; oneResult[depth] = nums[i]; vector<int> numsTmp(nums); numsTmp.erase(numsTmp.begin() + i); dfs(numsTmp, oneResult, depth + 1, length, result); } } vector<vector<int>> permuteUnique(vector<int>& nums) { sort(nums.begin(), nums.end()); int length = nums.size(); vector<int> oneResult(length, 0); vector<vector<int>> result; int depth = 0; dfs(nums, oneResult, depth, length, result); return result; }};
0 0
- 【LeetCode】Permutations && Permutations II
- leetcode: Permutations/Permutations II
- leetcode Permutations & Permutations II
- [Leetcode]Permutations && Permutations II
- Permutations and Permutations II
- Permutations && Permutations ii
- Permutations II
- Permutations II
- Permutations II
- Permutations II
- Permutations II
- Permutations II
- Permutations II
- Permutations II
- Permutations II
- Permutations II
- Permutations II
- Permutations II
- 关于solr schema.xml 和solrconfig.xml的解释
- 让你在求职中脱颖而出的小技巧
- 【sdk】Eclipse更新sdk,设置红杏公益代理
- 在eclipse中导出javadoc报"错误: 编码GBK的不可映射字符"解决办法
- 三段保留的私有地址
- Permutations II
- Windows下Node.js安装和配置
- javascript基础(三)
- solr suggest配置
- MATLAB中取整函数(fix, floor, ceil, round)的使用
- handler.post和handler.sendMessage的区别和联系
- MATLAB程序设计
- Thinking In Java<<Java编程思想>>
- SQL Server控制语句