47. 全排列 II

题意

给定一个可包含重复数字的序列,返回所有不重复的全排列。

示例:

输入: [1,1,2]
输出:
[
[1,1,2],
[1,2,1],
[2,1,1]
]

解题思路

去重的全排列就是从第一个数字起每个数分别与它后面非重复出现的数字交换。用编程的话描述就是第i个数与第j个数交换时,要求[i,j)中没有与第j个数相等的数。有两种方法(1)可以每次在需要交换时进行顺序查找;(2)用哈希表来查重;

  1. 回溯:遍历数组,判断是否能够交换,再两两交换给定对应下标的值;

  2. 回溯:思想和1一样;

  3. 回溯:遍历数组,两两交换给定对应下标的值,在加入最终结果的时候判断是否已经存在;

  4. 记忆化:通过遍历当前路径数组,遍历当前的路径数组选择位置来插入index对应的值实现;

实现


class Solution(object):
def permuteUnique(self, nums):
       """
      :type nums: List[int]
      :rtype: List[List[int]]
      """

       def swap(start, end):
           nums[start], nums[end] = nums[end], nums[start]

       def can_swap(start, end):
           for i in range(start, end):
               if nums[i] == nums[end]:
                   return False
           return True

       def helper(index):
           if index == len(nums):
               res.append(nums[:])
               return

           for i in range(index, len(nums)):
               # 避免出现相同的子数组
               is_swap = can_swap(index, i)
               if is_swap:
                   print is_swap
                   swap(i, index)
                   helper(index + 1)
                   swap(index, i)

   def permuteUnique(self, nums):
       """
      :type nums: List[int]
      :rtype: List[List[int]]
      """
       def helper(nums, index):
           if index >= len(nums):
               res.append(nums[:])
               return

           for i in range(index, len(nums)):
               if i != index and nums[i] == nums[index]:
                   continue
# 有个问题,为什么在交换之后没有交换回来,是因为从一开始排序以后,就没有打算交换回来?
               # 我的猜测是避免再次交换导致的重复的值出现
               nums[i], nums[index] = nums[index], nums[i]
               helper(nums[:], index + 1)

       res = []
       nums.sort()
       helper(nums, 0)
       return res
     
def permuteUnique(self, nums):
       """
      :type nums: List[int]
      :rtype: List[List[int]]
      """
       self.result=[]
       self.helper(nums,0)
       
       return self.result


   def helper(self,nums,i):
       if i ==len(nums)-1:
           self.result.append(nums[:])
       used=set()
       for j in range(i,len(nums)):
           if nums[j] not in used:
               used.add(nums[j])
               nums[i],nums[j]=nums[j],nums[i]
               self.helper(nums[:],i+1)
               nums[i],nums[j]=nums[j],nums[i]
     
def permuteUnique(self, nums):
       """
      :type nums: List[int]
      :rtype: List[List[int]]
      """
       if not nums:
           return []
       
       res = [[]]
       for n in nums:
           temp = []
           for cur in res:
               for i in range(len(cur)+1):
                   temp.append(cur[:i] + [n] + cur[i:])
                   if i < len(cur) and cur[i] == n:  # remove dups
                       break
           res = temp
       return res

47. 全排列 II的更多相关文章

  1. Leetcode之回溯法专题-47. 全排列 II(Permutations II)

    Leetcode之回溯法专题-47. 全排列 II(Permutations II) 给定一个可包含重复数字的序列,返回所有不重复的全排列. 示例: 输入: [1,1,2] 输出: [ [1,1,2] ...

  2. [LeetCode] 47. 全排列 II

    题目链接 : https://leetcode-cn.com/problems/permutations-ii/ 题目描述: 给定一个可包含重复数字的序列,返回所有不重复的全排列. 示例: 输入: [ ...

  3. Java实现 LeetCode 47 全排列 II(二)

    47. 全排列 II 给定一个可包含重复数字的序列,返回所有不重复的全排列. 示例: 输入: [1,1,2] 输出: [ [1,1,2], [1,2,1], [2,1,1] ] class Solut ...

  4. leetcode 46. 全排列 及 47. 全排列 II

    46. 全排列 问题描述 给定一个没有重复数字的序列,返回其所有可能的全排列. 示例: 输入: [1,2,3] 输出: [ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3 ...

  5. LeetCode 47 全排列II

    题目: 给定一个可包含重复数字的序列,返回所有不重复的全排列. 示例: 输入: [1,1,2] 输出: [ [1,1,2], [1,2,1], [2,1,1] ] 解题思路: 与上一题相比,这题多了一 ...

  6. Leetcode题库——47.全排列II

    @author: ZZQ @software: PyCharm @file: permuteUnique.py @time: 2018/11/16 13:34 要求:给定一个可包含重复数字的序列,返回 ...

  7. LeetCode 47. 全排列 II(Permutations II)

    题目描述 给定一个可包含重复数字的序列,返回所有不重复的全排列. 示例: 输入: [1,1,2] 输出: [ [1,1,2], [1,2,1], [2,1,1] ] 解题思路 类似于LeetCode4 ...

  8. 47全排列II

    题目:给定一个可包含重复数字的序列,返回所有不重复的全排列. 示例: 输入:[1,1,2]输出:[[1,1,2],[1,2,1],[2,1,1]] 来源:https://leetcode-cn.com ...

  9. LeetCode 47——全排列 II

    1. 题目 2. 解答 在 LeetCode 46--全排列 中我们已经知道,全排列其实就是先确定某一个位置的元素,然后余下就是一个子问题.在那个问题中,数据没有重复,所以数据中的任意元素都可以放在最 ...

随机推荐

  1. nodejs的process模块如何获取其他进程的pid

    var cmd=process.platform=='win32'?'tasklist':'ps aux'; var exec = require('child_process').exec; var ...

  2. Python3学习笔记25-logging模块

    logging模块,Python自带用来记录日志的模块. 因为工作需要用到关于日志的,最近一直都在看关于日志模块的东西,百度了很多文章,可惜都是看的让人一头雾水,最后运气不错,找到一篇很详细的文章.传 ...

  3. 现代C++之理解auto类型推断

    理解auto类型推断 上一篇帖子中讲述了模板类型推断,我们知道auto的实现原理是基于模板类型推断的,回顾一下模板类型推断: template <typename T> void f(Pa ...

  4. java FTPClient 上传文件 0kb 问题

    解决方法: 1.本地防火墙关闭了2.服务端端防火墙关闭 CentOS 7.0关闭默认防火墙启用iptables防火墙 操作系统环境:CentOS Linux release 7.0.1406(Core ...

  5. android 手机拍照返回 Intent==null 以及intent.getData==null

    手机拍照第一种情况:private void takePicture(){ Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);Si ...

  6. vw 解决方案

    vw 解决方案 1. 安装并配置PostCss插件 复制代码代码如下: npm i postcss-aspect-ratio-mini postcss-px-to-viewport postcss-w ...

  7. PYTHON-模块 json pickle shelve xml

    """ pickle 和 shevle 序列化后得到的数据 只有python才能解析 通常企业开发不可能做一个单机程序 都需要联网进行计算机间的交互 我们必须保证这个数据 ...

  8. 在vscode成功配置Python环境

    注意:如果您希望在Visual Studio Code中开始使用Python,请参阅教程.本文仅关注设置Python解释器/环境的各个方面. Python中的“环境”是Python程序运行的上下文.环 ...

  9. java多线程快速入门(六)

    多线程应用实例(批量发送短信) 1.创建实体类 package com.cppdy; public class UserEntity { private int id; private String ...

  10. Promise in Chakra

    http://www.ecma-international.org/ecma-262/#sec-fulfillpromise 25.4.1.3.1 and 25.4.1.3.2 Promise Rej ...