Permutations

1.题目

给定一个数字列表,返回其所有可能的排列

2.算法

算法一:这是一个np问题,方法还是原来那个套路,还是用一个循环递归处理子问题。前面的数有可能放到后面,所以我们需要维护一个used数组来表示该元素是否已经在当前结果中,因为每次我们取一个元素放入结果,然后递归剩下的元素,所以不会出现重复。

public ArrayList<ArrayList<Integer>> permute(int[] num) {  
    ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>>();  
    if(num==null || num.length==0)  
        return res;  
    helper(num, new boolean[num.length], new ArrayList<Integer>(), res);  
    return res;  
}  
private void helper(int[] num, boolean[] used, ArrayList<Integer> item, ArrayList<ArrayList<Integer>> res)  
{  
    if(item.size() == num.length)  
    {  
        res.add(new ArrayList<Integer>(item));  
        return;  
    }  
    for(int i=0;i<num.length;i++)  
    {  
        if(!used[i])  
        {  
            used[i] = true;  
            item.add(num[i]);  
            helper(num, used, item, res);  
            item.remove(item.size()-1);  
            used[i] = false;  
        }  
    }  
} 

算法二:假如不用递归来作,我们用迭代来做

这道题的基本思路是,每遍历数组中的一个元素,我们把他加到已经生成的全排列的每个位置

public ArrayList<ArrayList<Integer>> permute(int[] num) {  
    ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>>();  
    if(num == null || num.length==0)  
        return res;  
    ArrayList<Integer> first = new ArrayList<Integer>();  
    first.add(num[0]);  
    res.add(first);  
    for(int i=1;i<num.length;i++)  
    {  
        ArrayList<ArrayList<Integer>> newRes = new ArrayList<ArrayList<Integer>>();  
        for(int j=0;j<res.size();j++)  
        {  
            ArrayList<Integer> cur = res.get(j);  
            for(int k=0;k<cur.size()+1;k++)  
            {  
                ArrayList<Integer> item = new ArrayList<Integer>(cur);  
                item.add(k,num[i]);  
                newRes.add(item);  
            }  
        }  
        res = newRes;  
    }  
    return res;     
} 


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值