Permutations I

Given a collection of distinct 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].

回溯法

思路

与subset不同的是对于数组[1,2,3] [2,1,3]和[2,3,1]等都算在排列里, 所以我们递归的的起始点不再是i+1, 而是每次都从0 开始.
但是由于每次都从0开始就没法知道是否要访问的下一个数之前有没有被用过, 所以这里用一个Boolean数组来存储每一个点的访问情况

复杂度

时间O(n!) 空间栈 O(n!)

代码

public List<List<Integer>> permute(int[] nums) {List<List<Integer>> res= new ArrayList<List<Integer>>();if (nums == null || nums.length == 0) {return res;}List<Integer> tem = new ArrayList<Integer>();Arrays.sort(nums);boolean[] visit = new boolean[nums.length];helper(nums, visit, res, tem);return res;
}
public void helper(int[] nums, boolean[] visit, List<List<Integer>> res, List<Integer> tem) {if (tem.size() == nums.length) {res.add(new ArrayList<Integer>(tem));return;}for (int i = 0; i < nums.length; i++) {if (visit[i] == true) {continue;}visit[i] = true;tem.add(nums[i]);helper(nums, visit, res, tem);tem.remove(tem.size() - 1);visit[i] = false;}
}

Permutations II

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

For example, [1,1,2] have the following unique permutations: [1,1,2],
[1,2,1], and [2,1,1].

回溯法

思路

与I不同的是这里允许了重复的元素 ,所以如果当前访问元素与之前元素相同而之前的元素没有访问过 说明相似的情况已经出现过 要避免再出现

复杂度

时间O(n!) 空间栈 O(n!)

代码

public List<List<Integer>> permuteUnique(int[] nums) {List<List<Integer>> res= new ArrayList<List<Integer>>();if (nums == null || nums.length == 0) {return res;}List<Integer> tem = new ArrayList<Integer>();Arrays.sort(nums);boolean[] visit = new boolean[nums.length];helper(nums, visit, res, tem);return res;
}
public void helper(int[] nums, boolean[] visit, List<List<Integer>> res, List<Integer> tem) {if (tem.size() == nums.length) {res.add(new ArrayList<Integer>(tem));return;}for (int i = 0; i < nums.length; i++) {if (visit[i] == true) {continue;}if (i != 0 && nums[i] == nums[i - 1] && !visit[i - 1]) {continue;}visit[i] = true;tem.add(nums[i]);helper(nums, visit, res, tem);tem.remove(tem.size() - 1);visit[i] = false;}
}

Permutations I II leetcode相关推荐

  1. LeetCode | Permutations I,II

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

  2. [Lintcode]115. Unique Paths II/[Leetcode]63. Unique Paths II

    115. Unique Paths II/63. Unique Paths II 本题难度: Easy/Medium Topic: Dynamic Programming Description Fo ...

  3. Remove Duplicates from Sorted Array II -- LeetCode

    原题链接: http://oj.leetcode.com/problems/remove-duplicates-from-sorted-array-ii/  这道题跟Remove Duplicates ...

  4. LeetCode 59. 螺旋矩阵 II LeetCode 54. 螺旋矩阵

    文章目录 1. 题目信息 2. LeetCode 59 解题 3. LeetCode 54. 螺旋矩阵 4.<剑指Offer>面试题29 1. 题目信息 给定一个正整数 n,生成一个包含 ...

  5. 贪心——跳跃游戏 II(Leetcode 45)

    题目选自 Leetcode 45. 跳跃游戏 || 与跳跃游戏 | 的不同之处在于,我们需要求的是最少的跳跃次数~ 题目描述: 解题思路: 思想 就一句话:每次在上次能跳到的范围(end)内选择一个能 ...

  6. Reverse Linked List II -- LeetCode

    原标题链接: http://oj.leetcode.com/problems/reverse-linked-list-ii/  这道题是比較常见的链表反转操作,只是不是反转整个链表.而是从m到n的一部 ...

  7. 992. Sort Array By Parity II - LeetCode

    为什么80%的码农都做不了架构师?>>>    Question 992. Sort Array By Parity II Solution 题目大意:给一个int数组,一半是奇数一 ...

  8. Unique Binary Search Trees II -- LeetCode

    原题链接:  http://oj.leetcode.com/problems/unique-binary-search-trees-ii/   这道题是求解所有可行的二叉查找树,从 Unique Bi ...

  9. Path Sum II leetcode java

    题目: Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the give ...

最新文章

  1. 单片机与微处理器和微型计算机的关系,微处理器、CPU、微处理机、微机、单片机它们之间有何区别?...
  2. 全年营业额怎么计算_会计税法计算公式大全
  3. Vim - 视图模式
  4. 【工具类】时间相关的方法
  5. Leetcode 226. 翻转二叉树 (每日一题 20210819)
  6. equipment download和CRM 的change history
  7. LoadRunner中进程运行和线程运行区别
  8. python神经网络作用_Python · 神经网络(三*)· 网络
  9. 收藏 | 一文带你深入理解深度学习最新进展
  10. matlab gul介绍及串口通信实现,Matlab - GUl介绍及串口通信实现(转)
  11. 截取字符串,计算字符串字节大小,选择文件夹,上传文件
  12. EMNLP 2021 投稿FAQ
  13. python 定义一个学生类、包含三个属性并打印输出_Python3.x基础学习-类--面向对象...
  14. mysql索引冲突_mysql 锁问题 (相同索引键值或同一行或间隙锁的冲突)
  15. 数据库、SID实例、Oracle数据库、sys、system用户
  16. centos安装activitymq
  17. 山海演武传·黄道·第一卷 雏龙惊蛰 第十三章 穷奇长梦(上) 十四 穷奇长梦(下)...
  18. dx逆向建模步骤_初级反求逆向建模(一)
  19. java操作RabbitMq时出现Caused by: org.springframework.amqp.AmqpException: Cannot determine ReplyTo message
  20. 股票爆仓有几种情况?股票爆仓具体怎么预防?

热门文章

  1. matlab preloadfcn,simulink中打不开SysytemGenerator?返回错误Error evaluating ...
  2. 专升本c语言程序设计网课_2020年宜春学院专升本招生信息
  3. ieda中快捷搜索_IntelliJ IDEA IDE设置系列教程(十):在工具窗口中快速搜索
  4. HDLBits 系列(28)PS/2 mouse protocol(PS/2 packet parser)
  5. 模6计数器以及模10计数器(Verilog HDL语言设计)(Modelsim仿真与ISE综合)
  6. 用PlanAhead进行RTL代码开发与分析
  7. ios 工具大全,最全框架
  8. 学嵌入式Linux软件开发需要的知识
  9. mac OS Sierra支持破解程序
  10. 洛谷 P1008 三连击 Label:水