Source of the topic:
https://leetcode.com/problems/permutations-ii/
Test Instructions Analysis:
Given a string of numbers that may have a repetition, return its full array.
Topic Ideas:
This problem is similar to the previous one, which can be solved by using the second method of the previous question directly. That is, given an arrangement, return to the next permutation situation.
Code (Python):
classsolution (object):defnextp (self,nums): Size= Len (nums); i = Size-2 whileI >=0:ifNums[i] < Nums[i + 1]: J= i + 1 whileJ <Size:ifNums[i] >=Nums[j]: BreakJ+ = 1J-= 1Nums[i],nums[j]=Nums[j],nums[i] ans= nums[:i + 1] + nums[:i:-1] returnans i-= 1ans= Nums[::-1] returnansdefPermuteunique (Self, nums):""": Type Nums:list[int]: Rtype:list[list[int]"""size=Len (nums)ifSize = =0:return[] Nums.sort (); TMP= Nums[:];ans =[] Ans.append (nums) whiletrue:tmp=SELF.NEXTP (TMP)ifTMP! =Nums:t=tmp[:] Ans.append (t)Else: Break returnAns
View Code
Reprint Please specify source: http://www.cnblogs.com/chruny/p/4953738.html
[Leetcode] (python): 047-permutations II