• 递归和回溯_leetcode46-经典的排列去重


    #coding=utf-8
    class Solution1(object):
    def permuteUnique(self, nums):
    """
    :type nums: List[int]
    :rtype: List[List[int]]
    """
    self.res = []

    nums.sort()
    length = len(nums)

    self.used = [False for i in range(length)]

    self.generatePermute(nums,0,[])

    if 0:
    print "lx"
    if 1:
    print "dd"


    print self.res
    return self.res

    # 去重思路:同一个位置的值不能出现两次,即下一个进入该位置的值,不能
    # 和该位置刚出栈的值相同

    def generatePermute(self,nums,index,p):

    if index == len(nums):
    self.res.append(p[0:])
    return

    old = None

    for i in range(len(nums)):

    # if old:
    # if not self.used[i] and nums[i] != old:
    # p.append(nums[i])
    # self.used[i] = True
    # self.generatePermute(nums,index+1 ,p)
    # self.used[i] = False
    # old = p.pop()
    # else:
    # if not self.used[i]:
    # p.append(nums[i])
    # self.used[i] = True
    # self.generatePermute(nums,index+1 ,p)
    # self.used[i] = False
    # old = p.pop()

    #if old != None:
    if old: # 这句有坑 原因在于 if 0 是假的
    if self.used[i] == False and old != nums[i]:
    p.append(nums[i])
    self.used[i] = True
    self.generatePermute(nums, index + 1, p)
    self.used[i] = False
    old = p.pop()

    else:
    if self.used[i] == False:
    p.append(nums[i])
    self.used[i] = True
    self.generatePermute(nums, index + 1, p)
    self.used[i] = False
    old = p.pop()



    # solution1 是错误的
    class Solution2(object):
    def permuteUnique(self, nums):
    """
    :type nums: List[int]
    :rtype: List[List[int]]
    """
    self.res = []

    nums.sort()
    self.visit = [0 for i in range(len(nums))]
    self.old = None
    for i in range(len(nums)):
    if self.old == None:
    self.generatePermute(nums,i,0,[])
    self.old = nums[i]
    else:
    if nums[i] == self.old:
    continue
    else:
    self.generatePermute(nums, i, 0, [])
    self.old = nums[i]


    print self.res

    def generatePermute(self, nums,start,index, p):

    self.visit[start] = 1
    p.append(nums[start])

    self.getPermute(nums,index+1,p)

    self.visit[start] = 0
    p.pop()


    def getPermute(self, nums, index, p):

    if index == len(nums):
    self.res.append(p[0:])
    return

    for i in range(len(nums)):

    if self.visit[i] == 0:
    p.append(nums[i])
    self.visit[i] = 1
    self.getPermute(nums, index + 1, p)
    self.visit[i] = 0
    self.old = p.pop()






    # Solution1 的思路是正确的
    # Solution2 是错的
    class Solution3(object):
    def permuteUnique(self, nums):
    """
    :type nums: List[int]
    :rtype: List[List[int]]
    """
    self.res = []

    nums.sort()
    self.visit = [0 for i in range(len(nums))] self.old = None self.generatePermute(nums,0,[]) print self.res def generatePermute(self, nums,index, p): if index == len(nums): self.res.append(p[0:]) old = None for i in range(len(nums)): if old == None: if self.visit[i] == 0: p.append(nums[i]) self.visit[i] = 1 self.generatePermute(nums,index+1,p) self.visit[i] = 0 old = p.pop() else: if self.visit[i] == 0 and nums[i] != old: p.append(nums[i]) self.visit[i] = 1 self.generatePermute(nums,index+1 ,p) self.visit[i] = 0 old = p.pop()s = Solution1()n = [1,1,2]n1 = [0,1,0,0,9]n2 = [0,1,0,0]n3 = [0,0,1]n4 = [1,2,3]n5 = [2,3,2]n6 = [1,1]s.permuteUnique(n6)
  • 相关阅读:
    log4j不生成日志文件的问题
    前端得到或者改变组件的值
    取list的值
    idea Library XXXXXXXX has broken classes paths
    session.save()返回值问题
    Intellij Error:Cannot build Artifact 'XXX:war exploded' because it is included into a circular dependency
    shell小结
    python操作rabbitMQ小结
    python+rabbitMQ实现生产者和消费者模式
    Python操作rabbitmq消息队列持久化
  • 原文地址:https://www.cnblogs.com/lux-ace/p/10556949.html
Copyright © 2020-2023  润新知