LeetCode-Permutation

5 篇文章 0 订阅
1 篇文章 0 订阅
作者:disappearedgod
时间:2014-9-28

题目

Permutations

  Total Accepted: 25153  Total Submissions: 80140 My Submissions

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

想法

这里是一道全排列的题目,剑指offer中的第28题字符串的排列,以及第12题:打印1到最大的n位数中都有涉及。

代码

public class Solution {
    List<List<Integer>> retlist = new LinkedList<List<Integer>>();
    public List<List<Integer>> permute(int[] num) {
        List<Integer> numlist = new LinkedList<Integer>();
        
        if(num.length ==0)
            return retlist;
        
        
        permutation(num, 0);
        return retlist;
    }
    void permutation(int[] num, int n){
        if(n == num.length-1){
            List<Integer> numlist = new LinkedList<Integer>();
            for(int i = 0; i<num.length; ++i)
                numlist.add(num[i]);
            retlist.add(numlist);
        }
        else{
            for(int i = n; i < num.length ; i++){
                swapArr(num, i, n);
                permutation(num, n+1);
                swapArr(num, i, n);
            }
        }
    }
    void swapArr(int[] a, int i, int j){
        int tmp = a[i];
        a[i] = a[j];
        a[j] = tmp;
    }
}



结果

My Submissions for Permutations
Submit Time Status Run Time Language
2 minutes ago Accepted 460 ms java

另一种想法

这里用一个数组来记录是否扫描过,依靠boolean数组中的true个数来使得递归收敛。
注意:
1.这里用的arraylist的对象唯一,所以添加元素时候要用他们的copy的实例,并且要维护arraylist本身(remove)
2.这里用的时一种叫做近距离递归的不好的编程风格。
3.结束条件,收敛证明是递归的必备

代码

public class Solution {
    ArrayList<ArrayList<Integer>> retlist = new ArrayList<ArrayList<Integer>>();
    boolean[] used;
    public ArrayList<ArrayList<Integer>> permute(int[] num) {
        ArrayList<Integer> list = new ArrayList<Integer>();
        if(num.length ==0)
            return retlist;
        used = new boolean[num.length == 0 ? 1 : num.length];
        
        
        
        //permutation(num, 0);
        permute(num, num.length, list);
        
        return retlist;
    }
    void permute(int[] num, int remind, ArrayList<Integer> list){
        if(remind == 0){
            ArrayList<Integer> copy =  (ArrayList<Integer>) list.clone();
            retlist.add(copy);
            //or retlist.add(new ArrayList<Integer>(list));
        }
       
        for(int i = 0; i < num.length ; ++i){
            if(used[i])
                continue;
            list.add(num[i]);
            used[i] = true;
            permute(num, remind-1 , list);
            list.remove(list.size()-1);
            
            used[i] = false;
        }
    }
}


结果


My Submissions for Permutations
Submit Time Status Run Time Language
9 minutes ago Accepted 436 ms java

返回


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值