全排列12 · Permutations
无重复
[抄题]:
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。反复用,所以叫回溯法
[一句话思路]:
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:


[一刷]:
- 递归退出用return;表示 递归推出的出口在helper函数中,不在每个点加进permutation的过程中
- 结果是复数数组,返回results 如果nums == null,返回[] 如果nums.length == 0, 返回[[]](result中添加一个空数组,返回result)
- ist.size()的效果等于nums.length
- helper调用整个数组时,参数是数组名
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
- 搜索递归的三个步骤:新参数、递归、去掉新参数
- 不知道回溯法:和求子集一样-[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]//
)
在不是第零位(没有前一位)的前提下,如果两数字相同,前面一个数却没有访问,此时不可
[一刷]:
- 有数组要先排序
- 设置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的更多相关文章
- Leetcode之回溯法专题-46. 全排列(Permutations)
Leetcode之回溯法专题-46. 全排列(Permutations) 给定一个没有重复数字的序列,返回其所有可能的全排列. 示例: 输入: [1,2,3] 输出: [ [1,2,3], [1,3, ...
- [Swift]LeetCode47. 全排列 II | Permutations II
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- LeetCode 46. 全排列(Permutations)
题目描述 给定一个没有重复数字的序列,返回其所有可能的全排列. 示例: 输入: [1,2,3] 输出: [ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [ ...
- 刷题——有重复元素的全排列(Permutations II)
题目如上所示. 我的解决方法(参考了九章的答案!): class Solution { public: /* * @param : A list of integers * @return: A li ...
- [CareerCup] 9.5 Permutations 全排列
9.5 Write a method to compute all permutations of a string. LeetCode上的原题,请参加我之前的博客Permutations 全排列和P ...
- LeetCode 046 Permutations
题目要求:Permutations(全排列) Given a collection of numbers, return all possible permutations. For example, ...
- HDOJ-ACM1016(JAVA) 字典序全排列,并剪枝
转载声明:原文转自http://www.cnblogs.com/xiezie/p/5576273.html 题意: 一个环是用图中所示的n个圆组成的.把自然数1.2.…….n分别放入每个圆中,并在相邻 ...
- 给定一个正整数,实现一个方法求出离该整数最近的大于自身的 换位数 <把一个整数各个数位进行全排列>
"""给定一个正整数,实现一个方法求出离该整数最近的大于自身的 换位数 -> 把一个整数各个数位进行全排列""" # 使用 permu ...
- UVALive 6909 Kevin's Problem 数学排列组合
Kevin's Problem 题目连接: https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid ...
随机推荐
- (转)Win7 64位系统下 Retional rose 2003 安装及破解
网上关于Retional rose 2003安装和破解的文章比较多,这里,我结合自己的亲身体验,和大家分享一下win7 旗舰版 64位系统下Retional rose 2003(下面简称rose200 ...
- javascript事件处理程序的3个阶段
第一阶段:HTML事件处理阶段.就是在元素里面添加onclick之类的属性来调用某个函数. <input type="button" value="单击" ...
- Oracle直方图的详细解析(转)
Oracle直方图解析 一. 何谓直方图: 直方图是一种统计学上的工具,并非Oracle专有.通常用于对被管理对象的某个方面的质量情况进行管理,通常情况下它会表现为一种几何图形表,这个图形表是根 ...
- Spark 分布式SQL引擎
SparkSQL作为分布式查询引擎:两种方式 SparkSQL作为分布式查询引擎:Thrift JDBC/ODBC服务 SparkSQL作为分布式查询引擎:Thrift JDBC/ODBC服务 Spa ...
- X-Requested-With
最近工作中发现,使用angular $http跨域的时候,虽然后台已经配置了跨域允许,但是还是报错. 查资料发现,angular $http 的request的请求头中,默认有: Access-Con ...
- 常用数据库2 sqlite及SQL注入
知识内容: 1.sqlite数据库介绍 2.sqlite数据库操作 3.SQL注入 一.sqlite数据库介绍 1.sqlite数据库 sqlite数据库:轻量级的数据库,一般开发中使用sqlite数 ...
- 0_Simple__simpleStreams
对比使用单流和多流(4条)情况下数据拷贝,以及数据拷贝加内核调用的效率差别.▶ 源代码 #include <stdio.h> #include <cuda_runtime.h> ...
- sparkcontext 和 sparkconf
http://spark.apache.org/docs/latest/rdd-programming-guide.html The first thing a Spark program must ...
- linux获取日志指定行数范围内的内容
假如我要获取“浅浅岁月拂满爱人袖”到“落入凡尘伤情着我”之间的内容. 1.首先得到他们所在的行号: -n选项显示行号 但是有时候grep后显示的是“匹配到二进制文件(标准输入)”,说明搜索的字符串在某 ...
- springboot sybase 数据库
依赖:(驱动) <!-- https://mvnrepository.com/artifact/net.sourceforge.jtds/jtds --> <dependency&g ...