[Leetcode] (python): 047-permutations II

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.