[Leedcode 46] Permutations

Source: Internet
Author: User

Given A collection of numbers, return all possible permutations.

For example,
[1,2,3]The following permutations:
[1,2,3],,,,, [1,3,2] [2,1,3] [2,3,1] [3,1,2] and [3,2,1] .

 Public classSolution {//full arrangement: Constructs a recursive function, a parameter of a function that represents the index to begin arranging, an index that represents the final arrangement//each one is exchanged with the start bit, and then recursively start+1 to end, paying attention to the saving of the resultList<integer>seq; List<List<Integer>>Res;  PublicList<list<integer>> Permute (int[] nums) {SEQ=NewArraylist<integer>(); Res=NewArraylist<list<integer>>(); Findpermute (Nums,0,nums.length-1); returnRes; }     Public voidFindpermute (int[]nums,intStartintend) {        if(start>end) {Res.add (Newarraylist<integer> (seq));//Note to re-new a            return ; }                for(inti=start;i<=end;i++) {swap (nums,start,i);            Seq.add (Nums[start]); Findpermute (Nums,start+1, end); Seq.remove (Seq.size ()-1);//Note Deleteswap (nums,start,i); }    }     Public voidSwapint[] Nums,intIintj) {        inttemp=Nums[i]; Nums[i]=Nums[j]; NUMS[J]=temp; }}

[Leedcode 46] Permutations

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.