题目

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

题解

这道题就用循环递归解决子问题。

因为求所有组合,这就意味着不能重复使用元素,要用visited数组。

有因为是所有可能的组合,所以循环length次,就是这里面每位都有可能有length个可能性。

正因为如此,每一层递归就不需要传递一个start点,告诉他从哪开始(因为都是从头开始循环)。

代码如下:

 1     public ArrayList<ArrayList<Integer>> permute(int[] num) {
 2         ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>>();
 3         ArrayList<Integer> item = new ArrayList<Integer>();
 4         
 5         if(num.length==0||num==null)
 6             return res;
 7         boolean[] visited = new boolean[num.length];  
 8         
 9         permutation_helper(num,res,item,visited);
         return res;
     }
     
     public void permutation_helper(int[] num, ArrayList<ArrayList<Integer>> res, ArrayList<Integer> item,boolean[] visited){
         if(item.size()==num.length){
             res.add(new ArrayList<Integer>(item));
             return;
         }
         
         for(int i = 0; i<num.length;i++){
             if(!visited[i]){
                 item.add(num[i]);
                 visited[i]=true;
                 permutation_helper(num,res,item,visited);
                 item.remove(item.size()-1);
                 visited[i]=false;
             }
         }
     }

Permutations leetcode java的更多相关文章

  1. N-Queens II leetcode java

    题目: Follow up for N-Queens problem. Now, instead outputting board configurations, return the total n ...

  2. Permutations II leetcode java

    题目: Given a collection of numbers that might contain duplicates, return all possible unique permutat ...

  3. Regular Expression Matching leetcode java

    题目: Implement regular expression matching with support for '.' and '*'. '.' Matches any single chara ...

  4. Sqrt(int x) leetcode java

    Reference: http://blog.csdn.net/lbyxiafei/article/details/9375735  题目: Implement int sqrt(int x). Co ...

  5. ZigZag Conversion leetcode java

    题目: The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows l ...

  6. [LeetCode][Java]Candy@LeetCode

    Candy There are N children standing in a line. Each child is assigned a rating value. You are giving ...

  7. [Leetcode][JAVA] Valid Palindrome

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...

  8. Permutations [LeetCode]

    Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the follow ...

  9. 【目录】LeetCode Java实现

    这里记录一下自己刷的LeetCode题目. 有些博客用英文阐述自己的思路和收获,相当于练习一下英文的表达能力. 比较好的题目有加粗. 1. Two Sum 3. Longest Substring W ...

随机推荐

  1. Linux块设备驱动详解

    <机械硬盘> a:磁盘结构 -----传统的机械硬盘一般为3.5英寸硬盘,并由多个圆形蝶片组成,每个蝶片拥有独立的机械臂和磁头,每个堞片的圆形平面被划分了不同的同心圆,每一个同心圆称为一个 ...

  2. RubyMine8 安装

    在win7下面安装RubyMine8 中文 汉化 1.需要下载 RubyMine8的程序包 2.先安装好 RubyMine8  很简单  点击下一步就可以  选择安装目录这应该就不需要说了 3.汉化 ...

  3. SWD and JTAG selection mechanism

    SWD and JTAG selection mechanism SWJ-DP enables either an SWD or JTAG protocol to be used on the deb ...

  4. Lodop客户端本地和集中打印 [是否安装][操作系统]

    前面有些博文或图片介绍了这两种方式,可能文字太多,耐心看完的人不多,这里简略描述+图片的方式解释一下. 客户端本地打印: 用户访问某网站,用自己本地的打印机打出来. 集中打印方式: 用户访问某网页,用 ...

  5. hdu 4643 GSM 计算几何 - 点线关系

    /* hdu 4643 GSM 计算几何 - 点线关系 N个城市,任意两个城市之间都有沿他们之间直线的铁路 M个基站 问从城市A到城市B需要切换几次基站 当从基站a切换到基站b时,切换的地点就是ab的 ...

  6. WCF技术我们应该如何以正确的方式去学习掌握

    一.WCF技术我该如何学习? 阿笨的回答是:作为初学者的我们,那么请跟着阿笨一起玩WCF吧,阿笨将带领大家如何以正确的姿势去掌握WCF技术.由于WCF技术知识点太多了,就纯基础概念性知识都可以单独出一 ...

  7. Java嵌入式数据库H2学习总结(二)——在Web应用程序中使用H2数据库

    一.搭建测试环境和项目 1.1.搭建JavaWeb测试项目 创建一个[H2DBTest]JavaWeb项目,找到H2数据库的jar文件,如下图所示: H2数据库就一个jar文件,这个Jar文件里面包含 ...

  8. C#编程(十七)----------Object类

    Object类 它是.NET Framework 中所有类的最终基类:它是类型层次结构的根.也就是说所有的类都拥有object类的方法,并能重写,调用. object的构造函数:public Obje ...

  9. Java 数组的 12 个最佳方法

    1.  声明一个数组 String[] aArray = new String[5]; String[] bArray = {"a","b","c&q ...

  10. android ActionBarImpl can only be used with a compatible window decor layout

    在Activity中添加 requestWindowFeature(Window.FEATURE_ACTION_BAR); requestWindowFeature(Window.FEATURE_AC ...