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

  • 相关阅读:
    飞控相关资料
    PID
    详解NXP Cortex-M3加密设置
    ucos ii 百度官方介绍
    两个静态页面之间值传递方式
    Wex5各组件介绍
    链接学习
    WeX5基础
    Oracle中用触发器实现自动记录表数据被修改的历史信息
    HTML DOM setInterval() 方法
  • 原文地址:https://www.cnblogs.com/chruny/p/4953738.html
Copyright © 2011-2022 走看看