zoukankan      html  css  js  c++  java
  • 【LeetCode】Permutations

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

    public class Solution {
        public ArrayList<ArrayList<Integer>> permute(int[] num) {
            ArrayList<ArrayList<Integer>> re =new ArrayList<ArrayList<Integer>>();
            if(num.length==0)
                return re;
            ArrayList<Integer> ai = new ArrayList<Integer>();
            ArrayList<ArrayList<Integer>> te =new ArrayList<ArrayList<Integer>>();
            ai.add(num[0]);
            re.add(ai);
            te.add(ai);
            if(num.length==1)
                return re;
            for(int i=1;i<num.length;i++){
                int t = num[i];
                Iterator<ArrayList<Integer>> it = re.iterator();
                te =new ArrayList<ArrayList<Integer>>();
                while(it.hasNext()){
                    ai = it.next();
                    for(int j=0;j<ai.size();j++){
                        ai.add(j, t);
                        ArrayList<Integer> tem = new ArrayList<Integer>();
                        for(int mm=0;mm<ai.size();mm++)
                            tem.add(ai.get(mm));
                        te.add(tem);
                        ai.remove(j);
                    }
                    ArrayList<Integer> ttem = new ArrayList<Integer>();
                    for(int nn=0;nn<ai.size();nn++)
                        ttem.add(ai.get(nn));
                    ttem.add(t);
                    te.add(ttem);
                    
                }
                re=te;
                te=new ArrayList<ArrayList<Integer>>();
            }
            return re;
            
        }
    }
  • 相关阅读:
    如何学习新技术
    创建模式之工厂方法模式
    SQL Server 存储过程
    ASP.NET Cache的一些总结
    ACE_TSS研究
    利用Thunk让C++成员函数变回调函数
    ACE内存映射学习
    ACE的初始化
    双检锁模式学习
    ACE_Task笔记
  • 原文地址:https://www.cnblogs.com/yixianyixian/p/3735696.html
Copyright © 2011-2022 走看看