[LeetCode] 47. 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],
[2,1,1]
]
46. Permutations 的拓展,这题数组含有重复的元素。解法和46题,主要是多出处理重复的数字。
先对nums排序,使得相同的元素排在一起。新建一个大小与nums相同visited数组,用来标记在本次DFS读取中,位置i的元素是否已经被添加。
如果当前的数与前一个数相等,并且前一个数未被添加到list中,则可跳过这个数。
C++:
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] 46. Permutations 全排列
[LeetCode] 31. Next Permutation 下一个排列
[LeetCode] 60. Permutation Sequence 序列排序
All LeetCode Questions List 题目汇总
[LeetCode] 47. Permutations II 全排列 II的更多相关文章
- [LeetCode] 47. Permutations II 全排列之二
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- LeetCode 47 Permutations II(全排列)
题目链接: https://leetcode.com/problems/permutations-ii/?tab=Description 给出数组,数组中的元素可能有重复,求出所有的全排列 使 ...
- leetCode 47.Permutations II (排列组合II) 解题思路和方法
Permutations II Given a collection of numbers that might contain duplicates, return all possible un ...
- LeetCode(47):全排列 II
Medium! 题目描述: 给定一个可包含重复数字的序列,返回所有不重复的全排列. 示例: 输入: [1,1,2] 输出: [ [1,1,2], [1,2,1], [2,1,1] ] 解题思路: 这道 ...
- [leetcode] 47. Permutations II
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- [leetcode]47. Permutations全排列(给定序列有重复元素)
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- LeetCode 46 Permutations(全排列问题)
题目链接:https://leetcode.com/problems/permutations/?tab=Description Problem:给出一个数组(数组中的元素均不相同),求出这个数组 ...
- [LeetCode] 267. Palindrome Permutation II 回文全排列 II
Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empt ...
- Leetcode之回溯法专题-47. 全排列 II(Permutations II)
Leetcode之回溯法专题-47. 全排列 II(Permutations II) 给定一个可包含重复数字的序列,返回所有不重复的全排列. 示例: 输入: [1,1,2] 输出: [ [1,1,2] ...
随机推荐
- eclipse项目结构目录
文章:eclipse web 项目目录结构 地址:https://blog.csdn.net/Alan_Wdd/article/details/90514928 eclipse web 项目目录结构 ...
- jmeter+nmon+crontab简单的执行接口定时压测
一.概述 临时接到任务要对系统的接口进行压测,上面的要求就是:压测,并发2000 在不熟悉系统的情况下,按目前的需求,需要做的步骤: 需要有接口脚本 需要能监控系统性能 需要能定时执行脚本 二.观察 ...
- Kotlin反射在属性上的应用实战
继续研究Kotlin反射相关的东东,看代码: 接下来则遍历函数,来调一下,比如咱们先来调用一下带有2个参数的method方法,可以这样写: 其实在调用实例方法时的第一个参数永远都是要调用的那个实例,也 ...
- 微信小程序之执行环境
明白了小程序中的 JavaScript 同浏览器以及NodeJS有所不同后,开发者还需要注意到另外一个问题,不同的平台的小程序的脚本执行环境也是有所区别的. 小程序目前可以运行在三大平台: iOS平台 ...
- bat echo 每行不同的颜色
bat echo 每行不同的颜色 先看代码: @echo off SETLOCAL EnableDelayedExpansion for /F "tokens=1,2 delims=#&qu ...
- Git学习笔记--历史与安装(一)
声明:今天起学习Git,第一篇学习笔记主要借鉴廖雪峰先生的个人博客,以及自己的实践所得. “本教程只会让你成为Git用户,不会让你成为Git专家”——引自廖雪峰博客. 一.Git简介 Git是目前世界 ...
- SpringBoot整合Bubbo
一.创建springboot_dubbo_provider项目 1 创建service层接口 public interface IDoSomeService { public String sayHi ...
- LOJ P10018 数的划分 题解
每日一题 day52 打卡 Analysis 这道题直接搜索会TLE到**,但我们发现有很多没有用的状态可以删去,比如 1,1,5; 1,5,1; 5,1,1; 所以很容易想到一个优化:按不下降的顺序 ...
- ssh集成
导入pom依赖 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w ...
- 百度地图中如何获取到发布的SHA1
百度地图中如何获取到发布的SHA1 下面介绍的是一种通过命令的方式获取到发布版SHA1的方法: 打开Android的命令行Terminal: 1.首先进入到.android文件所在的目录,我的是如下图 ...