首页 > 代码库 > [LeetCode] Permutations

[LeetCode] Permutations

 1 public class Solution { 2     public List<List<Integer>> permute(int[] num) { 3         List<List<Integer>> result = new ArrayList<List<Integer>>(); 4         permute(result, num, 0); 5         return result; 6     } 7      8     public void permute(List<List<Integer>> result, int [] num, int startIndex) { 9         if (startIndex == num.length) {10             List<Integer> oneResult = new ArrayList<Integer>();11             for(int i=0; i<num.length; i++)12                 oneResult.add(num[i]);13             result.add(oneResult);14         }15         for (int i=startIndex; i<num.length; i++) {16             swap(num, startIndex, i);17             permute(result, num, startIndex+1);18             swap(num, startIndex, i);19         }20     }21 22     public void swap(int [] num, int index1, int index2) {23         int tmp = num[index1];24         num[index1] = num[index2];25         num[index2] = tmp;26     }27 28 }

 

[LeetCode] Permutations