![](https://www.dazhuanlan.com/webchat.jpg)
Given a collection of distinct numbers, return all possible permutations.
For example,
[1,2,3]
have the following permutations:
1 2 3 4 5 6 7 8
|
[ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1] ]
|
Solution
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
public class { public List<List<Integer>> permute(int[] nums) { LinkedList<List<Integer>> result = new LinkedList<List<Integer>>(); result.add(new ArrayList<Integer>()); for(int n : nums) { int size = result.size(); for(;size>0;size--) { List<Integer> each = result.pollFirst(); for(int i=0; i<=each.size();i++) { List<Integer> e = new ArrayList<Integer>(each); e.add(i,n); result.add(e); } } } return result; } }
|
近期评论