码迷,mamicode.com
首页 > 编程语言 > 详细

[LeetCode]题解(python):047-Permutations II

时间:2015-11-10 19:19:02      阅读:307      评论:0      收藏:0      [点我收藏+]

标签:

题目来源:

  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

[LeetCode]题解(python):047-Permutations II

标签:

原文地址:http://www.cnblogs.com/chruny/p/4953738.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!