码迷,mamicode.com
首页 > 其他好文 > 详细

[LeetCode] Permutations

时间:2014-09-07 17:17:15      阅读:164      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   io   ar   for   art   div   sp   

 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

标签:style   blog   color   io   ar   for   art   div   sp   

原文地址:http://www.cnblogs.com/yuhaos/p/3960553.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!