无重复

[抄题]:

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

[思维问题]:

不知道回溯法:和求子集一样-[1],[1,2]-只剩[1]

123用了2,312又要用2。反复用,所以叫回溯法

[一句话思路]:

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

  1. 递归退出用return;表示 递归推出的出口在helper函数中,不在每个点加进permutation的过程中
  2. 结果是复数数组,返回results 如果nums == null,返回[] 如果nums.length == 0, 返回[[]](result中添加一个空数组,返回result)
  3. ist.size()的效果等于nums.length
  4. helper调用整个数组时,参数是数组名

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

  1. 搜索递归的三个步骤:新参数、递归、去掉新参数
  2. 不知道回溯法:和求子集一样-[1],[1,2]-只剩[1]

[复杂度]:Time complexity: O() Space complexity: O()

[英文数据结构或算法,为什么不用别的数据结构或算法]:

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

public class Solution {
/*
* @param nums: A list of integers.
* @return: A list of permutations.
*/
public List<List<Integer>> permute(int[] nums) {
//corner case
List<List<Integer>> results = new ArrayList<>();
List<Integer> permutations = new ArrayList<>();
HashSet<Integer> set = new HashSet<>(); if (nums == null) {
return null;
}
if (nums.length == 0) {
//return new ArrayList<>();
results.add(new ArrayList<>());
return results;
}
//helper
helper(nums, permutations, set, results);
//return
return results;
}
//helper
public void helper (int[] nums, List<Integer> permutations,
HashSet<Integer> set, List<List<Integer>> results) {
if (permutations.size() == nums.length) {
results.add(new ArrayList<>(permutations));
return ;//
} for (int i = 0; i < nums.length; i++) {
if (set.contains(nums[i])) {
continue;
}
permutations.add(nums[i]);
set.add(nums[i]);
helper(nums, permutations, set, results);
set.remove(nums[i]);
permutations.remove(permutations.size() - 1);
}
}
}

有重复

[抄题]:

[思维问题]:

有重复元素不知道是默认不排序的,要先排序

[一句话思路]:

排序后用visited数组来控制

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

((i != 0 && visited[i - 1] == 0) &&
nums[i - 1] == nums[i]//
)

在不是第零位(没有前一位)的前提下,如果两数字相同,前面一个数却没有访问,此时不可

[一刷]:

  1. 有数组要先排序
  2. 设置visited数组默认为0

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

不要把visited nums数组名写错了

[总结]:

代码风格:太长可以换行、不要用很多的&& ||符号

[复杂度]:Time complexity: O(分支的深度次方) Space complexity: O(深度*分支)

[英文数据结构或算法,为什么不用别的数据结构或算法]:

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

31. Next Permutation 如果不是要求找出全部排列的,就几乎都是做数组调整 玩文字游戏

public class Solution {
/*
* @param nums: A list of integers.
* @return: A list of permutations.
*/
public List<List<Integer>> permuteUnique(int[] nums) {
//corner case
List<List<Integer>> results = new ArrayList<>();
List<Integer> permutations = new ArrayList<>();
int[] visited = new int[nums.length]; if (nums == null) {
return null;
}
if (nums.length == 0) {
results.add(new ArrayList<>());
return results;
}
//helper
Arrays.sort(nums);
for (int i = 0; i < nums.length; i++) {
visited[i] = 0;
}
helper(nums, permutations, visited, results);
//return
return results;
}
//helper
public void helper (int[] nums, List<Integer> permutations,
int[] visited, List<List<Integer>> results) {
if (permutations.size() == nums.length) {
results.add(new ArrayList<>(permutations));
return ;//
} for (int i = 0; i < nums.length; i++) {
if (visited[i] == 1) {
continue;
}
if ((i != 0 && visited[i - 1] == 0) &&
nums[i - 1] == nums[i]//
) {
continue;
}
visited[i] = 1;
permutations.add(nums[i]);
helper(nums, permutations, visited, results);
permutations.remove(permutations.size() - 1);
visited[i] = 0;
}
}
}

全排列12 · Permutations的更多相关文章

  1. Leetcode之回溯法专题-46. 全排列(Permutations)

    Leetcode之回溯法专题-46. 全排列(Permutations) 给定一个没有重复数字的序列,返回其所有可能的全排列. 示例: 输入: [1,2,3] 输出: [ [1,2,3], [1,3, ...

  2. [Swift]LeetCode47. 全排列 II | Permutations II

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

  3. LeetCode 46. 全排列(Permutations)

    题目描述 给定一个没有重复数字的序列,返回其所有可能的全排列. 示例: 输入: [1,2,3] 输出: [ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [ ...

  4. 刷题——有重复元素的全排列(Permutations II)

    题目如上所示. 我的解决方法(参考了九章的答案!): class Solution { public: /* * @param : A list of integers * @return: A li ...

  5. [CareerCup] 9.5 Permutations 全排列

    9.5 Write a method to compute all permutations of a string. LeetCode上的原题,请参加我之前的博客Permutations 全排列和P ...

  6. LeetCode 046 Permutations

    题目要求:Permutations(全排列) Given a collection of numbers, return all possible permutations. For example, ...

  7. HDOJ-ACM1016(JAVA) 字典序全排列,并剪枝

    转载声明:原文转自http://www.cnblogs.com/xiezie/p/5576273.html 题意: 一个环是用图中所示的n个圆组成的.把自然数1.2.…….n分别放入每个圆中,并在相邻 ...

  8. 给定一个正整数,实现一个方法求出离该整数最近的大于自身的 换位数 <把一个整数各个数位进行全排列>

    """给定一个正整数,实现一个方法求出离该整数最近的大于自身的 换位数 -> 把一个整数各个数位进行全排列""" # 使用 permu ...

  9. UVALive 6909 Kevin's Problem 数学排列组合

    Kevin's Problem 题目连接: https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid ...

随机推荐

  1. Windows下python 安装Mysqldb模块

    CMD执行 pip install mysql-python 报错如下: 1.如果报类似 Microsoft Visual C++ 9.0 is required < Unable to fin ...

  2. VMware vCenter Server安装

    SQL Server 2008 R2安装包:http://blog.sina.com.cn/s/blog_4aedf6370101j9tz.html SQL Server安装指导:http://www ...

  3. dubbo无法创建线程问题

    OutOfMemoryError: unable to create new native thread 决定当前用户程序能够创建多少线程由2个因素决定 1. 用户环境允许的线程数 cat /etc/ ...

  4. MySQL工具 Navicat

    F6打开命令行窗口 ctrl+shift+r  运行选中语句   #不过不建议使用,因为ctrl+r是执行当前所有语句,如果语句当中有delete update 后果不堪设想

  5. Ubuntu12.10下Vsftpd的安装

    安装Vsftpd sudo apt-get install vsftpd 配置 sudo vim /etc/vsftpd.conf 取消以下两行前面的注释 local_enable=YES write ...

  6. unity3d将C#打包成dll方法

    方法一:用vs新建工程-C#库,添加UnityEngine.dll引用,注意.netframwork选3.5,编译C#脚本得到dll: 方法二:使用mono的mcs,具体如下 c#提供了dll打包,但 ...

  7. Impala源码分析

    问题导读:1.Scheduler任务中Distributed Plan.Scan Range是什么?2.Scheduler基本接口有哪些?3.QuerySchedule这个类如何理解?4.Simple ...

  8. (15/24) 为webpack增加babel支持

    Babel是什么? Babel是一个编译JavaScript的平台,它的强大之处表现在可以通过编译达到以下目的: 使用下一代的javaScript代码(ES6,ES7-.),即使这些标准目前并未被当前 ...

  9. leetcode35

    public class Solution { public int SearchInsert(int[] nums, int target) { ; i < nums.Length; i++) ...

  10. eclipse xml 编码问题 “3 字节的 UTF-8 序列的字节 3 无效”

    原本项目没问题,git commit之后,突然报错 “3 字节的 UTF-8 序列的字节 3 无效” 尝试过改xml文件编码等,没成功.pom中设置属性,成功解决 <project.build. ...