标签:
Given a collection of distinct 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].
1 public class Solution { 2 public List<List<Integer>> permute(int[] nums) { 3 int len = nums.length; 4 List<List<Integer>> ans = new ArrayList<List<Integer>>(); 5 ans.add(new ArrayList<Integer>()); 6 for(int i = 0; i < len; i++) 7 { 8 List<List<Integer>> new_ans = new ArrayList<List<Integer>>(); 9 for(int j = 0; j <= i; j++) 10 { 11 for(int k = 0; k < ans.size(); k++) 12 { 13 List<Integer> temp = new ArrayList<Integer>(ans.get(k)); 14 temp.add(j,nums[i]); 15 new_ans.add(temp); 16 } 17 } 18 ans = new_ans; 19 } 20 return ans; 21 } 22 23 24 }
标签:
原文地址:http://www.cnblogs.com/vin-yuan/p/5439616.html