题目

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. Codeforces Round #501 (Div. 3) F. Bracket Substring

    题目链接 Codeforces Round #501 (Div. 3) F. Bracket Substring 题解 官方题解 http://codeforces.com/blog/entry/60 ...

  2. Loj 10211 sumdiv

    题目描述 求 A^B 的所有约数之和 mod 9901. 首先,我们要求出A的约数之和. 就是把A分解质因数,成为:a1^k1*a2^k2*a3^k2.... 然后约数和就是(a1^0+a1^1+a1 ...

  3. 模板 倍增维护RMQ

    倍增维护RMQ,nlogn预处理,O(1)查询 #include<bits/stdc++.h> using namespace std; const int maxn = 1e5+7; s ...

  4. Java Web c3p0 pool池泄漏优化与日志分析

    问题跟踪: 近期在整合SSH(spring.springmvc.hibernate)项目,提供给第三方服务.每当调用内存池达到上限之后,外界调用服务直接失败,提示[cannot open connec ...

  5. 使用Python中的HTMLParser、cookielib抓取和解析网页、从HTML文档中提取链接、图像、文本、Cookies(二)(转)

    对搜索引擎.文件索引.文档转换.数据检索.站点备份或迁移等应用程序来说,经常用到对网页(即HTML文件)的解析处理.事实上,通过 Python语言提供的各种模块,我们无需借助Web服务器或者Web浏览 ...

  6. 巧用CSS3 :target 伪类制作Dropdown下拉菜单(无JS)

    :target 是CSS3 中新增的一个伪类,用以匹配当前页面的URI中某个标志符的目标元素(比如说当前页面URL下添加#comment就会定位到id=“comment”的位置,俗称锚).CSS3 为 ...

  7. 对 Git 分支 master 和 origin/master 的一些认识

    首先要明确一点,对 Git 的操作是围绕 3 个大的步骤来展开的(其实几乎所有的 SCM 都是这样) 从 git 取数据(git clone) 改动代码 将改动传回 git(git push) 这 3 ...

  8. [Go] Http包 使用简介

    请求的结构 HTTP 的交互以请求和响应的应答模式.Go 的请求我们早就见过了,handler 函数的第二个参数 http.Requests.其结构为: type Request struct { M ...

  9. Revit Family API 添加参数与尺寸标注

    使用FamilyManager其他的与普通添加参数与标注没区别. [TransactionAttribute(Autodesk.Revit.Attributes.TransactionMode.Man ...

  10. DELPHI之崩溃地址排错代码查看 转

    http://www.cnblogs.com/enli/archive/2009/01/15/1376540.html 最近研究了一下HOOK技术,想抓取某些游戏的包,因此需要注入DLL,结果老是有异 ...