"046-permutations (order)"
" leetcode-interview algorithm classic-java Implementation" "All topic Directory Index"
Original title
Given A collection of numbers, return all possible permutations.
For example,
[1,2,3] have the following permutations:
[1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], and [3,2,1].
The main effect of the topic
Given an array, returns all of his permutations.
ideas for solving problems
Solved by using the method of divide and conquer.
Code Implementation
Algorithm implementation class
Import java.util.*;
public class Solution {
private list<list<integer>> result;
Public list<list<integer>> permute (int[] num) {result
= new linkedlist<> ();
if (num!= null) {
permute (0, num);
}
return result;
}
private void Permute (int i, int[] num) {
if (i = = num.length) {
list<integer> L = new arraylist<> (); for
(int n:num) {
l.add (n);
}
Result.add (l);
} else {for
(int j = i; J < Num.length; J + +) {
swap (num, j, i);
Permute (i + 1, num);
Swap (num, j, i);
}} private void Swap (int[] A, int x, int y) {
int tmp = a[x];
A[X] = A[y];
A[y] = tmp;
}
}
Evaluation Results
Click on the picture, the mouse does not release, drag a position, released in a new window to view the full picture.
Special Notes Welcome reprint, Reprint please indicate the source "http://blog.csdn.net/derrantcm/article/details/47098351"