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 ...
随机推荐
- BCH码
http://baike.baidu.com/link?url=CfLtm9DigwWdup-9VJP99RG65NgaVOXfrnjT61ogP7au0QOrlypq72k67B0s1Ey-Q1yD ...
- selenium的下拉选择框
今天总结下selenium的下拉选择框.我们通常会遇到两种下拉框,一种使用的是html的标签select,另一种是使用input标签做的假下拉框. 后者我们通常的处理方式与其他的元素类似,点击或使用J ...
- sql server数据库字段名要注意不能叫file
sql server数据库字段名要注意不能叫file 如java 中 private string file,这是sql 的关键字
- shell sed 命令
1:行首空格 sed 's/^[ \t]*//g' 2:行末空格 sed 's/[ \t]*$//g' 3,删除行首的空格或TAB,并删除<tr>.cat poem2id.txt | ...
- PAT 天梯赛 L1-007. 念数字 【水】
题目链接 https://www.patest.cn/contests/gplt/L1-007 AC代码 #include <iostream> #include <cstdio&g ...
- CodeForces - 987E Petr and Permutations (思维+逆序对)
题意:初始有一个序列[1,2,...N],一次操作可以将任意两个位置的值互换,Petr做3*n次操作:Alxe做7*n+1次操作.给出最后生成的新序列,问是由谁操作得到的. 分析:一个序列的状态可以归 ...
- 反射_获取字段的Description信息
var memInfo = enumType.GetType().GetMember(enumType.ToString()); var attributes = memInfo[0].GetCust ...
- Java多线程基础知识总结笔记
本篇笔记记录一些在Java多线程编程中常见的关键字,比较简单和基础的就不写太详细了. 一.Thread类(其实也是应用了Runnable接口)和Runnable接口(只有一个run方法,应用该类必须重 ...
- javascript设计模式 - 解释器模式(interpreter)
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Hive-行转列(explode)
场景:通过parseHtml UDF解析一串HTML,返回一以 @@ 分割的字符串,使用split分割字符串进数组中,然后将数组的元素转列. 开始的写法 SELECT id, legal_person ...