1. Title
Permutations
2. Http address
https://leetcode.com/problems/permutations/
3. The question
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]
.
Subscribe to see which companies asked this question
4. My code (AC)
1 public List<List<Integer>> permute(int[] nums) { 2 3 List<List<Integer>> re = null; 4 List<List<Integer>> bf = new ArrayList<List<Integer>>(); 5 Iterator<List<Integer>> it = null; 6 List<Integer> list =null; 7 List<Integer> addList =null; 8 9 if( nums == null || nums.length <= 0 ) 10 return bf; 11 12 list = new ArrayList<Integer>(); 13 list.add(nums[0]); 14 bf.add(list); 15 re = bf; 16 for(int i = 1; i < nums.length; i++) 17 { 18 19 it = bf.iterator(); 20 re = new ArrayList<List<Integer>>(); 21 while(it.hasNext()) 22 { 23 list = it.next(); 24 for(int j = 0 ; j <= list.size(); j++ ) 25 { 26 addList = new ArrayList<Integer>(list); 27 addList.add(j,nums[i]); 28 re.add(addList); 29 } 30 } 31 bf = re; 32 } 33 return re; 34 35 }
原文:http://www.cnblogs.com/ordili/p/4928481.html