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

Leetcode: Permutations

时间:2014-07-10 13:34:30      阅读:208      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   使用   os   

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].

Analysis: 这道题跟N-Queens,Sudoku Solver,Combination SumCombinations, Subsets, Generate Parentheses等一样,也是一个NP问题

又是把所有满足条件的结果存到一个ArrayList里面, 之前也有类似的如Letter combination of a Phone Number这道题。这种类型的题其实形成了一个套路,套路就是,recursion参数包括最终结果的集合(ArrayList),input(String),递归层次level(int),某一条具体的路径Path

Notice: 我在permute函数里面第9行到第16行的循环其实是不需要的, 而且使用boolean数组来记录访问某个节点与否似乎比添加删除arraylist要容易

 1 public class Solution {
 2     public ArrayList<ArrayList<Integer>> permute(int[] num) {
 3         ArrayList<ArrayList<Integer>> permutations = new ArrayList<ArrayList<Integer>>();
 4         ArrayList<Integer> permutation = new ArrayList<Integer>();
 5         ArrayList<Integer> collection = new ArrayList<Integer>();
 6         for (int each : num) {
 7             collection.add(each);
 8         }
 9         for (int i = 0; i < collection.size(); i++) {
10             int current = collection.get(i);
11             collection.remove(i);
12             permutation.add(current);
13             helper(permutations, permutation, current, collection);
14             permutation.remove(0);
15             collection.add(i, current);
16         }
17         return permutations;
18     }
19     
20     public void helper(ArrayList<ArrayList<Integer>> permutations, ArrayList<Integer> permutation, int current, ArrayList<Integer> collection) {
21         if (collection.isEmpty()) {
22             permutations.add(new ArrayList<Integer>(permutation));
23             return;
24         }
25         for (int j = 0; j < collection.size(); j++) {
26             current = collection.get(j);
27             collection.remove(j);
28             permutation.add(current);
29             helper(permutations, permutation, current, collection);
30             permutation.remove(permutation.size()-1);
31             collection.add(j, current);
32         }
33     }
34 }

别人的做法:

public class Solution {
    public ArrayList<ArrayList<Integer>> permute(int[] num) {
        ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();
        ArrayList<Integer> element = new ArrayList<Integer>();
        boolean[] visited = new boolean[num.length];
        helper(num, result, element, visited);
        return result;
    }
    
    public void helper(int[] num, ArrayList<ArrayList<Integer>> result, ArrayList<Integer> element, boolean[] visited){
        if (element.size() == num.length){
            // duplicate element and add it to result (element would be changed from time to time. If directly use element
            // only result would be changed when element changed)
            result.add(new ArrayList<Integer>(element)); 
            return;
        }
        
        for(int i=0; i<num.length; i++){
            if(!visited[i]){
                visited[i] = true;
                element.add(num[i]);
                helper(num, result, element, visited);
                
                // After providing a complete permutation, pull out the last number, 
                element.remove(element.size()-1);
                visited[i] = false;
            }
        }
    }
}

 

Leetcode: Permutations,布布扣,bubuko.com

Leetcode: Permutations

标签:style   blog   http   color   使用   os   

原文地址:http://www.cnblogs.com/EdwardLiu/p/3811782.html

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