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 List<List<Integer>> permute(int[] num) {
List<List<Integer>> answerList = new ArrayList<List<Integer>>();
ArrayList<Integer> currentList = new ArrayList<Integer>();
boolean[] visited = new boolean[num.length];
DS(answerList, visited, num, currentList);
return answerList;
} public void DS(List<List<Integer>> answerList,boolean[] visited,int[] num,ArrayList<Integer> currentList){
boolean find = true;
for(int i = 0;i < num.length;i++){
if(!visited[i]){
currentList.add(num[i]);
visited[i]= true;
DS(answerList, visited, num, currentList);
visited[i]= false;
currentList.remove(currentList.size()-1);
find = false;
}
}
if(find){
ArrayList <Integer> temp = new ArrayList<Integer>(currentList);
answerList.add(temp);
}
}
}

上述代码中DS函数为递归深度优先搜索函数,answerList记录最终得到的所有排列,visited数据记录在某个时间点对应节点是否在访问过的路径上,currentList为当前走过的路径。要注意的一点是找到一条新的路径后,要为这条路径申请内存空间存放它(代码第23行所示),否则currentList被修改的时候已经存放到answerList中的排列也会被修改。

【leetcode刷提笔记】Permutations的更多相关文章

  1. 【leetcode刷提笔记】Search Insert Position

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

  2. 【leetcode刷提笔记】Container With Most Water

    Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai).  ...

  3. LeetCode刷题笔记和想法(C++)

    主要用于记录在LeetCode刷题的过程中学习到的一些思想和自己的想法,希望通过leetcode提升自己的编程素养 :p 高效leetcode刷题小诀窍(这只是目前对我自己而言的小方法,之后会根据自己 ...

  4. 【leetcode刷题笔记】Permutations II

    Given a collection of numbers that might contain duplicates, return all possible unique permutations ...

  5. LeetCode刷题笔记 - 12. 整数转罗马数字

    学好算法很重要,然后要学好算法,大量的练习是必不可少的,LeetCode是我经常去的一个刷题网站,上面的题目非常详细,各个标签的题目都有,可以整体练习,本公众号后续会带大家做一做上面的算法题. 官方链 ...

  6. 18.9.10 LeetCode刷题笔记

    本人算法还是比较菜的,因此大部分在刷基础题,高手勿喷 选择Python进行刷题,因为坑少,所以不太想用CPP: 1.买股票的最佳时期2 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. ...

  7. LeetCode刷题笔记(1-9)

    LeetCode1-9 本文更多是作为一个习题笔记,没有太多讲解 1.两数之和 题目请点击链接 ↑ 最先想到暴力解法,直接双循环,但是这样复杂度为n平方 public int[] twoSum(int ...

  8. leetcode刷题笔记

    (1)Best Time to Buy and Sell Stock Total Accepted: 10430 Total Submissions: 33800My Submissions Say ...

  9. leetcode刷题笔记08 字符串转整数 (atoi)

    题目描述 实现 atoi,将字符串转为整数. 在找到第一个非空字符之前,需要移除掉字符串中的空格字符.如果第一个非空字符是正号或负号,选取该符号,并将其与后面尽可能多的连续的数字组合起来,这部分字符即 ...

随机推荐

  1. SpringMVC 学习笔记(十一) SpirngMVC执行流程

    watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvYTY3NDc0NTA2/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA ...

  2. 征信报告页面的input验证收集

    https://ipcrs.pbccrc.org.cn/ function checkLoginName() { var loginName = $.trim($("#loginname&q ...

  3. 定时执行线程池ScheduledExecutorService的使用

    ScheduledExecutorService progressExecutorService = Executors.newScheduledThreadPool(1); ScheduledFut ...

  4. vivado2013.4和modelsim联合仿真

    vivado2013.4和modelsim联合仿真                           Hello,Panda        最近在做Zynq的项目,曾经尝试使用ISE+PlanAhe ...

  5. lua学习笔记(十三)

    math库     定义在math中     所有三角函数都使用弧度     指数和对数函数     取整函数     伪随机数math.random         调用时没有参数返回0~1之间的随 ...

  6. [译]GLUT教程 - 重整子窗体

    Lighthouse3d.com >> GLUT Tutorial >> Subwindows >> Reshape Subwindows 重整函数的回调需要处理两 ...

  7. [转载]Axis2 and CXF的比较

    在Celtix 和XFire 宣布合并的同年,另一个著名开源Web 服务框架Axis 的后继者Axis2 也诞生了.Axis2 并非Axis 的2.0 版,而是完全重写了Axis 的新项目.作为功能和 ...

  8. jquery validate 详解

    jQuery校验官网地址:http://bassistance.de/jquery-plugins/jquery-plugin-validation 一导入js库 <script src=&qu ...

  9. 多媒体开发之---h264格式slice_header

    从Slice_Header学习H.264 写在前面: $     H.264我是结合标准和毕厚杰的书一块学的.看句法语义时最是头疼,一大堆的元素,很需要耐心.标准中在介绍某个元素的语义时,经常会突然冒 ...

  10. UVa 10651 Pebble Solitaire(DP 记忆化搜索)

    Pebble Solitaire Pebble solitaire is an interesting game. This is a game where you are given a board ...