首页 > 其他 > 详细

Leetcode Permutations

时间:2016-04-27 18:46:09      阅读:266      评论:0      收藏:0      [点我收藏+]
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 }

技术分享

Leetcode Permutations

原文:http://www.cnblogs.com/vin-yuan/p/5439616.html

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