题目

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. 工程化框架之feather

    feather是一个工程化框架,他的主要任务是框架规范.性能优化.代码部署.自动化.本地调试.多人协同.静态资源管理. 一.安装 因为feather 为npm包,要安装node.js: 如果需要本地调 ...

  2. UVA.1640.The Counting Problem / BZOJ.1833.[ZJOI2010]数字计数(数位DP)

    题目链接 \(Description\) 求\([l,r]\)中\(0,1,\cdots,9\)每个数字出现的次数(十进制表示). \(Solution\) 对每位分别DP.注意考虑前导0: 在最后统 ...

  3. 【BZOJ-2595】游览计划 斯坦纳树

    2595: [Wc2008]游览计划 Time Limit: 10 Sec  Memory Limit: 256 MBSec  Special JudgeSubmit: 1518  Solved: 7 ...

  4. 吴恩达-coursera-机器学习-week2

    四.多变量线性回归(Linear Regression with Multiple Variables) 4.1 多维特征 4.2 多变量梯度下降 4.3 梯度下降法实践1-特征缩放 4.4 梯度下降 ...

  5. Centos部署使用Jexus承载asp.net core2 web应用

    一,首先安装本地开发项目用的的 core对应版本运行时: https://www.microsoft.com/net/download/linux-package-manager/centos/run ...

  6. FileReader读取中文txt文件编码丢失问题(乱码)(转)

    有一个UTF-8编码的文本文件,用FileReader读取到一个字符串,然后转换字符集:str=new String(str.getBytes(),"UTF-8");结果大部分中文 ...

  7. SGU 275. To xor or not to xor (高斯消元法)

    题目链接:http://acm.sgu.ru/problem.php?contest=0&problem=275 题意:给你n个数,可以选择任意个数异或,但是要使得最后的异或值最大. 我们把每 ...

  8. Timer-triggered memory-to-memory DMA transfer demonstrator

    http://www.efton.sk/STM32/bt.c // Timer-triggered memory-to-memory DMA transfer demonstrator for STM ...

  9. Visual Studio 2013 sqlce 配置(转)

    Visual Studio 2013 把內建 SQL CE 的管理工具拿掉了 下载SQL Server Compact Toolbox by ErikEJ并安装 打开VS2013,新建一工程,在“视图 ...

  10. Execute Javascript in iOS Applications

    In this tutorial, I have covered How to execute JavaScript in iOS / Objective-C. You can execute Jav ...