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. python根据服务名获取服务启动路径

    #coding=utf8 import _winreg as winreg class Win32Environment: """Utility class to get ...

  2. Android service与Thread

    很多时候,你可能会问,为什么要用 Service,而不用 Thread 呢,因为用 Thread 是很方便的,比起 Service 也方便多了,下面我详细的来解释一下. 1). Thread:Thre ...

  3. Expm 7_2区间调度问题

    [问题描述] 给定n个活动,其中的每个活动ai包含一个起始时间si与结束时间fi.设计与实现算法从n个活动中找出一个最大的相互兼容的活动子集S. 要求:分别设计动态规划与贪心算法求解该问题.其中,对贪 ...

  4. robotium之无name、ID仅有desc定位

    场景如图: 没有name和ID,群里问了,也没人搭理我,自己试验了下,发现这个法子可用,直接贴代码: Activity act = solo.getCurrentActivity(); int ide ...

  5. java中文GBK和UTF-8编码转换乱码的分析

    原文:http://blog.csdn.net/54powerman/article/details/77575656 作者:54powerman 一直以为,java中任意unicode字符串,可以使 ...

  6. python 全栈开发,Day130(多玩具端的遥控功能, 简单的双向聊天,聊天记录存放数据库,消息提醒,玩具主动发起消息,玩具主动发起点播)

    先下载github代码,下面的操作,都是基于这个版本来的! https://github.com/987334176/Intelligent_toy/archive/v1.3.zip 注意:由于涉及到 ...

  7. C# byte数组与Image的相互转换【转】

    功能需求: 1.把一张图片(png bmp jpeg bmp gif)转换为byte数组存放到数据库. 2.把从数据库读取的byte数组转换为Image对象,赋值给相应的控件显示. 3.从图片byte ...

  8. centOS下单点部署k8s

    Kubernetes 是Google的一种基于容器的开源服务编排解决方案,在我们进行Kubernetes的学习前,为了对Kubernetes的工作有一个大概的认识, 我们需要先安装一个单节点的实例服务 ...

  9. Android Rom build.prop文件详解

    # begin build properties   # autogenerated by buildinfo.sh   #以下内容由脚本在编译时自动产生 ro.build.id=6.7.7_97  ...

  10. Ubuntu 里面 apt-get 三个有关更新的命令的区别

    apt-get update 更新软件源中的所有软件列表. apt-get upgrade 更新软件. apt-get dist-upgrade 更新系统版本. 作者:耑新新,发布于  博客园 转载请 ...