zoukankan      html  css  js  c++  java
  • [LeetCode]题解(python):047-Permutations II

    题目来源:

      https://leetcode.com/problems/permutations-ii/


    题意分析:

      给定可能有重复的的一串数字,返回它的全排列。


    题目思路:

      这道题目和上一题类似,直接用上一题目的第二种方法就可以解决了。也就是给定一个排列情况,返回下一个排列的情况。


    代码(python):

      

    class Solution(object):
        def nextp(self,nums):
            size = len(nums);i = size - 2
            while i >= 0:
                if nums[i] < nums[i + 1]:
                    j = i + 1
                    while j < size:
                        if nums[i] >= nums[j]:
                            break
                        j += 1
                    j -= 1
                    nums[i],nums[j] = nums[j],nums[i]
                    ans = nums[:i + 1] + nums[:i:-1]
                    return ans
                i -= 1
            ans = nums[::-1]
            return ans
        def permuteUnique(self, nums):
            """
            :type nums: List[int]
            :rtype: List[List[int]]
            """
            size = len(nums)
            if size == 0:
                return []
            nums.sort();tmp = nums[:];ans = []
            ans.append(nums)
            while True:
                tmp = self.nextp(tmp)
                if tmp != nums:
                    t = tmp[:]
                    ans.append(t)
                else:
                    break
            return ans
    View Code

    转载请注明出处:http://www.cnblogs.com/chruny/p/4953738.html

  • 相关阅读:
    20161101学习笔记
    20161031学习笔记
    20161028学习笔记
    20161027学习笔记
    ReentrantLock Condition
    ReentrantLock 重入锁
    CountDownLatch用法与原理
    场景化解释 AQS原理
    Atomic
    多线程工具类
  • 原文地址:https://www.cnblogs.com/chruny/p/4953738.html
Copyright © 2011-2022 走看看