首页 > 代码库 > [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
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。