作者: 负雪明烛
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++)的更多相关文章

  1. 【LeetCode】Permutations II 解题报告

    [题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...

  2. LeetCode: Permutations II 解题报告

    Permutations II Given a collection of numbers that might contain duplicates, return all possible uni ...

  3. leetCode 47.Permutations II (排列组合II) 解题思路和方法

    Permutations II  Given a collection of numbers that might contain duplicates, return all possible un ...

  4. LeetCode: Unique Paths II 解题报告

    Unique Paths II Total Accepted: 31019 Total Submissions: 110866My Submissions Question Solution  Fol ...

  5. [LeetCode] 47. Permutations II 全排列 II

    Given a collection of numbers that might contain duplicates, return all possible unique permutations ...

  6. 【LeetCode】107. Binary Tree Level Order Traversal II 解题报告 (Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:DFS 方法二:迭代 日期 [LeetCode ...

  7. 【LeetCode】92. Reverse Linked List II 解题报告(Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 迭代 递归 日期 题目地址:https://leet ...

  8. 【LeetCode】82. Remove Duplicates from Sorted List II 解题报告(Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/remove-du ...

  9. [LeetCode] 47. Permutations II 全排列之二

    Given a collection of numbers that might contain duplicates, return all possible unique permutations ...

随机推荐

  1. (转载)java中判断字符串是否为数字的方法的几种方法

    java中判断字符串是否为数字的方法: 1.用JAVA自带的函数 public static boolean isNumeric(String str){ for (int i = 0; i < ...

  2. 简单的Mybatis程序

    1.新建一个普通Maven项目,导入 mybatis.mysql.junit(用于测试)3个依赖 Mybatis <dependency> <groupId>org.mybat ...

  3. 【风控算法】一、变量分箱、WOE和IV值计算

    一.变量分箱 变量分箱常见于逻辑回归评分卡的制作中,在入模前,需要对原始变量值通过分箱映射成woe值.举例来说,如"年龄"这一变量,我们需要找到合适的切分点,将连续的年龄打散到不同 ...

  4. aboard, abolish

    aboard board做动词有上车/船/飞机的意思,boarding就是正在上.board做名词有板的意思,车厢地板的板. a是个词根,有三种意思:1. 以某种状态或方式,如: ablaze, af ...

  5. day09 orm查询优化相关

    day09 orm查询优化相关 今日内容概要 orm字段相关补充 orm查询优化相关 orm事务操作 图书管理系统练习 今日内容详细 orm事务操作 """ 事务:ACI ...

  6. 零基础学习java------day16-----文件,递归,IO流(字节流读写数据)

    1.File 1.1 构造方法(只是创建已经存在文件的对象,并不能创建没有的文件) (1)public File(String pathname) (2)public File(String pare ...

  7. 2016广东工业大学新生杯决赛 A-pigofzhou的巧克力棒

    题目:GDUTOJ | pigofzhou的巧克力棒 (gdutcode.cn) 之前看了大佬博客的题解,一直没懂(我太菜了),后来听了朋友@77的讲解,终于懂了. 和拆分出2的n次方不一样,这是一种 ...

  8. python下载openpyxl

    直接下载openpyxl报错 ERROR: Command errored out with exit status 1: python setup.py egg_info Check the log ...

  9. ping (网络诊断工具)

    Ping是Windows.Unix和Lnix系统下的一个命令,ping也属于一个通信协议,是TCP/IP协议的一部分,利用Ping命令可以检查网络是否连通,可以很好地帮助我们分析和判定网络故障.应用格 ...

  10. OpenStack之六: plancement服务(端口8778)

    官网地址:https://docs.openstack.org/placement/stein/install/install-rdo.html #:创建placement库,并授权 MariaDB ...