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. phantomjs 中如何使用xpath

    function getNodeInfo(inputcsvPath) { var htmlnodeInfo = page.evaluate(function () { //_Ltg var XPATH ...

  2. expdp和impdp导入和导出数据

    一  关于expdp和impdp     使用EXPDP和IMPDP时应该注意的事项:EXP和IMP是客户端工具程序,它们既可以在客户端使用,也可以在服务端使用.EXPDP和IMPDP是服务端的工具程 ...

  3. 将本地光盘做成yum源

    环境:vmware 1.将centos6.5光盘挂载在虚拟机上 2.将光盘挂载在/mnt/cdrom目录下 root#  mkdir /mnt/cdrom root # mount /mnt/cdro ...

  4. MyBatis返回Map键值对数据

    List<Map<String, String>> getMtypeList(); <select id="getMtypeList" resultT ...

  5. androidpn 推送系统

    (文中部分内容来自网络,如无意中侵犯了版权,请告之!) XMPP协议: XMPP : The Extensible Messaging andPresence Protocol. 中文全称:可扩展通讯 ...

  6. jmeter之ip欺骗

    说明:我看有的博客说官方文档是在jmeter2.5以上的版本有此功能的实现~ 我的是2.13版本,也可以实现 . 准备工作: 使用IP欺骗功能必须得本地有多个可用IP,通常普通的PC机只有一个物理网卡 ...

  7. WPF设置对象隐藏、不可用

    设置隐藏时,这里将控件分为两类, 1.普通的按钮.下拉框等,根据控件的Name进行查找,设置IsEnabled为false; 2.ListView中嵌套控件,直接将列隐藏,根据GridViewColu ...

  8. java多线程快速入门(十五)

    使用violate关键字解决了变量的可见性问题(volatile让多线程刷新falg的值) package com.cppdy; class MyThread11 extends Thread { / ...

  9. C++ code:数组初始化

    具有初始化的数组定义,其元素个数可以省略,即方括号中的表达式可以省略.这时候,最后确定的元素个数取决于初始化值的个数.例如: #include<iostream> using namesp ...

  10. js----DOM对象(3

    表格示例(取消,全选,反选): <!DOCTYPE html> <html lang="en"> <head> <meta charset ...