首页 > 其他 > 详细

Permutations

时间:2015-11-01 20:59:05      阅读:282      评论:0      收藏:0      [点我收藏+]

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     }

 

Permutations

原文:http://www.cnblogs.com/ordili/p/4928481.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!