LeetCode OJ:Permutations II(排列II)
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].
依旧是dfs,不过这次要注意的是排列不允许有重复的vector存在,那么在排列之前应该先排序,然后每次检查的时候,如果前面一个是相同的元素而且已经用过了那么这个元素才可以用,如果不相同的元素那么直接dfs就可以用了,代码如下:
class Solution {
public:
vector<vector<int>> permuteUnique(vector<int>& nums) {
memset(cdds, , sizeof(cdds));
memset(mark, , sizeof(mark));
sort(nums.begin(), nums.end());
dfs(, nums.size(), nums);
return ret;
}
void dfs(int dep, int maxDep, vector<int> & nums)
{
if(dep == maxDep){
tmp.clear();
for(int i = ; i < maxDep; ++i)
tmp.push_back(cdds[i]);
ret.push_back(tmp);
}
for(int i = ; i < maxDep; ++i){
if(!mark[i]){
if(i != && nums[i] == nums[i - ] && !mark[i - ])
continue;
mark[i] = true;
cdds[dep] = nums[i];
dfs(dep + , maxDep, nums);
mark[i] = false;
}
}
}
private:
int cdds[];
bool mark[];
vector<int> tmp;
vector<vector<int>> ret;
};
做出这种限制的原因就是使得相同的数只存在一种排列顺序,这样就不会出现重复的组合这种情况了。
LeetCode OJ:Permutations II(排列II)的更多相关文章
- Leetcode 667.优美的排列II
优美的排列II 给定两个整数 n 和 k,你需要实现一个数组,这个数组包含从 1 到 n 的 n 个不同整数,同时满足以下条件: ① 如果这个数组是 [a1, a2, a3, ... , an] ,那 ...
- Java实现 LeetCode 667 优美的排列 II(暴力)
667. 优美的排列 II 给定两个整数 n 和 k,你需要实现一个数组,这个数组包含从 1 到 n 的 n 个不同整数,同时满足以下条件: ① 如果这个数组是 [a1, a2, a3, - , an ...
- 【LeetCode OJ】Path Sum II
Problem Link: http://oj.leetcode.com/problems/path-sum-ii/ The basic idea here is same to that of Pa ...
- 【LeetCode OJ】Word Ladder II
Problem Link: http://oj.leetcode.com/problems/word-ladder-ii/ Basically, this problem is same to Wor ...
- 【LeetCode OJ】Palindrome Partitioning II
Problem Link: http://oj.leetcode.com/problems/palindrome-partitioning-ii/ We solve this problem by u ...
- 【LEETCODE OJ】Single Number II
Problem link: http://oj.leetcode.com/problems/single-number-ii/ The problem seems like the Single Nu ...
- 【LeetCode OJ】Word Break II
Problem link: http://oj.leetcode.com/problems/word-break-ii/ This problem is some extension of the w ...
- LeetCode OJ——Pascal's Triangle II
http://oj.leetcode.com/problems/pascals-triangle-ii/ 杨辉三角2,比杨辉三角要求的空间上限制Could you optimize your algo ...
- LeetCode OJ 229. Majority Element II
Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorit ...
- [LeetCode OJ] Linked List Cycle II—Given a linked list, return the node where the cycle begins. If there is no cycle, return null.
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode ...
随机推荐
- 购物单问题—WPS使用excel
**** 180.90 88折 **** 10.25 65折 **** 56.14 9折 **** 104.65 ...
- 判断元素的16中方法expected_conditions
from selenium.webdriver.support import expected_conditons as EC 1.title_is:判断当前页面的title是否完全等于预期字符串,返 ...
- pandas(六)读写文本格式的数据
pandas提供的将表格型数据读取为DataFrame对象的函数. 函数 说明 read_csv 从文件.URL.文件型对象中加载带分隔符的数据.默认分隔符为逗号. read_table 从文件.UR ...
- Jupyter Notebook修改目标文件
默认的路径 如果没有修改配置文件,那么一般就在用户目录下面: 下面各处默认起始目标地址,以防有一天想改回来 I:\shujufenxi\python.exe I:\shujufenxi\cwp.py ...
- 什么是 jQuery 和jQuery的基本选择器,层级选择器,基本筛选器
jQuery是什么? [1] jQuery由美国人John Resig创建,至今已吸引了来自世界各地的众多 javascript高手加入其team. [2] jQuery是继prototype ...
- CodeForces - 894E Ralph and Mushrooms (强连通缩点+dp)
题意:一张有向图,每条边上都有wi个蘑菇,第i次经过这条边能够采到w-(i-1)*i/2个蘑菇,直到它为0.问最多能在这张图上采多少个蘑菇. 分析:在一个强连通分量内,边可以无限次地走直到该连通块内蘑 ...
- 正则表达式和python的re模块
0 正则表达式 0.1 常见的元字符 .: 匹配除\r\n之外的任何单个字符 *: 匹配前面的子表达式任意次,例如Zz*可以匹配Z,可以匹配Zz,也可以匹配Zzzzzzzzzz +: ...
- Mysql5.7 用户与授权
mysql -uroot -proot MySQL5.7 mysql.user表没有password字段改 authentication_string: 一. 创建用户: 命令:CREATE USER ...
- Qios RibbonForm QRibbonCaption添加qRibbonApplicationButton无法最大化问题
winform 用了Qios DevSuite系列的控件. RibbonForm中QRibbonCaption添加qRibbonApplicationButton之后无法最大化. 修改qRibbonA ...
- AFNetworking 3.0 解决加密后请求参数是字符串问题
把整个请求参数的json加密生成一个字符串传给服务器,错误提示:[NSJSONSerialization dataWithJSONObject:options:error:]: Invalid top ...