"046-permutations (for arrangement)"
"leetcode-Interview algorithm classic-java Implementation" "All topics Directory Index"
Original Question
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]
.
Main Topic
Given an array, returns all of his permutations.
Thinking of solving problems
It is solved by the method of divide and cure.
Code Implementation
Algorithm implementation class
Import java.util.*; Public classSolution {Privatelist<list<integer>> result; PublicList<list<integer>>Permute(int[] num) {result =NewLinkedlist<> ();if(num! =NULL) {Permute (0, num); }returnResult }Private void Permute(intIint[] num) {if(i = = num.length) {list<integer> L =NewArraylist<> (); for(intN:num) {l.add (n); } result.add (L); }Else{ for(intj = i; J < Num.length; J + +) {swap (num, j, i); Permute (i +1, num); Swap (num, j, i); } } }Private void Swap(int[] A,intXintY) {intTMP = a[x]; A[X] = A[y]; A[y] = tmp; }}
Evaluation Results
Click on the picture, the mouse does not release, drag a position, release after the new window to view the full picture.
Special Instructions
Welcome reprint, Reprint please indicate the source "http://blog.csdn.net/derrantcm/article/details/47098351"
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
"Leetcode-Interview algorithm classic-java Implementation" "046-permutations (Arrange)"