【LeetCode】47. Permutations II 解题报告(Python & C++)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址:https://leetcode.com/problems/permutations-ii/description/
题目描述
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]
]
题目大意
找出有可能有重复数字的一个数组的所有全排列。
解题方法
方法一:递归
之前的【LeetCode】46. Permutations 解题报告是没有重复数字的,这个题有重复数字。我的做法很简单,就是在以前的基础上加了一个判断条件:path not in res。这样的做法是在每个path生成之后才去做的判断,因此效率一点都不高。最后竟然也能通过了。
class Solution(object):
def permuteUnique(self, nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
res = []
self.helper(nums, res, [])
return res
def helper(self, nums, res, path):
if not nums and path not in res:
res.append(path)
else:
for i in range(len(nums)):
self.helper(nums[:i] + nums[i+1:], res, path + [nums[i]])
方法二:回溯法
上面的做法并不是标准的回溯法,标准的回溯法应该不更改nums。下面这个做法是标准的回溯法,需要用到visited来表示哪些位置已经被添加到path中了。
如何去重呢?我们想一想为什么会有重复出现:在这个例子中,我们在第一个1开始的排列中已经取了第二个1的情况;如果在第二个1开始的排列中仍然取第一个1,就有重复了。
所以,我们的做法是先对数组进行排序,保证相等的数字放在一起,然后当我们遇到的不是第一个数字,并且现在的数字和前面的数字相等,同时前面的数字还没有访问过,我们是不能搜索的,需要直接返回。原因是,这种情况下,必须是由前面搜索到现在的这个位置,而不能是由现在的位置向前面搜索。
C++代码如下:
class Solution {
public:
vector<vector<int>> permuteUnique(vector<int>& nums) {
const int N = nums.size();
sort(nums.begin(), nums.end());
vector<bool> visited(N, false);
vector<vector<int>> res;
helper(nums, res, {}, visited, 0);
return res;
}
void helper(vector<int>& nums, vector<vector<int>>& res, vector<int> path, vector<bool>& visited, int count) {
if (count == nums.size()) {
res.push_back(path);
return;
}
for (int i = 0; i < nums.size(); i++) {
if (visited[i]) continue;
if (i > 0 && nums[i] == nums[i - 1] && !visited[i - 1]) continue;
visited[i] = true;
path.push_back(nums[i]);
helper(nums, res, path, visited, count + 1);
path.pop_back();
visited[i] = false;
}
}
};
日期
2018 年 3 月 10 日
2018 年 12 月 21 日 —— 一周就要过去了
【LeetCode】47. Permutations II 解题报告(Python & C++)的更多相关文章
- 【LeetCode】Permutations II 解题报告
[题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...
- LeetCode: Permutations II 解题报告
Permutations II Given a collection of numbers that might contain duplicates, return all possible uni ...
- leetCode 47.Permutations II (排列组合II) 解题思路和方法
Permutations II Given a collection of numbers that might contain duplicates, return all possible un ...
- LeetCode: Unique Paths II 解题报告
Unique Paths II Total Accepted: 31019 Total Submissions: 110866My Submissions Question Solution Fol ...
- [LeetCode] 47. Permutations II 全排列 II
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- 【LeetCode】107. Binary Tree Level Order Traversal II 解题报告 (Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:DFS 方法二:迭代 日期 [LeetCode ...
- 【LeetCode】92. Reverse Linked List II 解题报告(Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 迭代 递归 日期 题目地址:https://leet ...
- 【LeetCode】82. Remove Duplicates from Sorted List II 解题报告(Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/remove-du ...
- [LeetCode] 47. Permutations II 全排列之二
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
随机推荐
- Shell中 ##%% 操作变量名
在linxu平台下少不了对变量名的处理,今天记录下shell中 ##%% 对变量名的操作. #操作左侧,%操作右侧. #号处理方式: 对于单个#,处理对象为变量中指定的第一个符号左侧字符串, 对于两个 ...
- Git配置文件与git config命令
在Git配置文件中配置变量,可以控制Git的外观和操作的各个方面.通过git config命令可以获得和设置配置变量. 一.Git配置文件的位置 这些变量可以被存储在三个不同的位置: 1./etc/g ...
- Can we use function on left side of an expression in C and C++?
In C, it might not be possible to have function names on left side of an expression, but it's possib ...
- java多线程 并发编程
一.多线程 1.操作系统有两个容易混淆的概念,进程和线程. 进程:一个计算机程序的运行实例,包含了需要执行的指令:有自己的独立地址空间,包含程序内容和数据:不同进程的地址空间是互相隔离的:进程拥有各种 ...
- ArrayList删除特定元素的方法
最朴实的方法,使用下标的方式: ArrayList<String> al = new ArrayList<String>(); al.add("a"); a ...
- Spring(3):AOP面向切面编程
一,AOP介绍 AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术.AOP是OOP的延续,是软件开 ...
- 【Linux】【Services】【KVM】virsh命令详解
1. virsh的常用命令 help:获取帮助 virsh help KEYWORD list:列出域 dumpxml:导出指定域的xml格式的配置文件: create:创建并启动域: define: ...
- 【Matlab】CFAR/phased.CFARDetector2D
| CFAR学习进行时ing... | CFAR原理.参数 检测阈值\(T = αP_n\) \(P_n\)是噪声功率估计,\(α\)是比例因子 训练单元:训练噪声,估计\(P_n = \frac{1 ...
- ASP.NET Core中使用滑动窗口限流
滑动窗口算法用于应对请求在时间周期中分布不均匀的情况,能够更精确的应对流量变化,比较著名的应用场景就是TCP协议的流量控制,不过今天要说的是服务限流场景中的应用. 算法原理 这里假设业务需要每秒钟限流 ...
- JuiceFS 缓存策略详解
对于一个由对象存储和数据库组合驱动的文件系统,缓存是本地客户端与远端服务之间高效交互的重要纽带.读写的数据可以提前或者异步载入缓存,再由客户端在后台与远端服务交互执行异步上传或预取数据.相比直接与远端 ...