Backtracking(一)
LeetCode中涉及到回溯的题目有通用的解题套路:
46. permutations
这一类回溯题目中的基础中的基础,无重复数字枚举:
/* 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],
[3,2,1]
] */ public class Solution {
public List<List<Integer>> permute(int[] nums) {
List<List<Integer>> res = new ArrayList<>();
backtrack(res, new ArrayList<>(), nums);
return res;
}
public void backtrack(List<List<Integer>> list, List<Integer> tmp, int[] nums){
if(tmp.size() == nums.length)
list.add(new ArrayList<>(tmp)); //要重新new一个list进去
else{
for(int i = 0; i < nums.length; i++){
if(tmp.contains(nums[i])) continue;
tmp.add(nums[i]);
backtrack(list, tmp, nums);
tmp.remove(tmp.size() - 1); //回溯的重要一步,恢复环境 }
}
}
}
47. 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],
[2,1,1]
] */ public class Solution {
public List<List<Integer>> permuteUnique(int[] nums) {
List<List<Integer>> res = new ArrayList<>();
if(nums == null || nums.length == 0) return res;
Arrays.sort(nums); //排序不要忘
backtrack(res, new ArrayList<>(), nums, new boolean[nums.length]);
return res;
}
public void backtrack(List<List<Integer>> list, List<Integer> tmp, int[] nums, boolean[] used){
if(tmp.size() == nums.length)
list.add(new ArrayList<>(tmp));
else{
for(int i = 0; i < nums.length; i++){
if(used[i] || (i > 0 && nums[i-1] == nums[i] && !used[i-1])) continue;
used[i] = true;
tmp.add(nums[i]);
backtrack(list, tmp, nums, used);
used[i] = false;
tmp.remove(tmp.size() - 1);
}
}
}
}
Backtracking(一)的更多相关文章
- LeetCode39/40/22/77/17/401/78/51/46/47/79 11道回溯题(Backtracking)
LeetCode 39 class Solution { public: void dfs(int dep, int maxDep, vector<int>& cand, int ...
- leetcode N-Queens/N-Queens II, backtracking, hdu 2553 count N-Queens, dfs 分类: leetcode hdoj 2015-07-09 02:07 102人阅读 评论(0) 收藏
for the backtracking part, thanks to the video of stanford cs106b lecture 10 by Julie Zelenski for t ...
- Backtracking line search的理解
使用梯度下降方法求解凸优化问题的时候,会遇到一个问题,选择什么样的梯度下降步长才合适. 假设优化函数为,若每次梯度下降的步长都固定,则可能出现左图所示的情况,无法收敛.若每次步长都很小,则下降速度非常 ...
- 重新发现梯度下降法--backtracking line search
一直以为梯度下降很简单的,结果最近发现我写的一个梯度下降特别慢,后来终于找到原因:step size的选择很关键,有一种叫backtracking line search的梯度下降法就非常高效,该算法 ...
- 【原创】回溯线搜索 Backtracking line search
机器学习中很多数值优化算法都会用到线搜索(line search).线搜索的目的是在搜索方向上找到是目标函数\(f(x)\)最小的点.然而,精确找到最小点比较耗时,由于搜索方向本来就是近似,所以用较小 ...
- backtracking问题
backtracking最基础的问题是Subsets,即给定一个数组,要求返回其所有子集. Given a set of distinct integers, nums, return all pos ...
- [LeetCode] 90.Subsets II tag: backtracking
Given a collection of integers that might contain duplicates, nums, return all possible subsets (the ...
- 数组排列组合问题——BACKTRACKING
BACKTRACKING backtracking(回溯法)是一类递归算法,通常用于解决某类问题:要求找出答案空间中符合某种特定要求的答案,比如eight queens puzzle(将国际象棋的八个 ...
- [LeetCode] 78. Subsets tag: backtracking
Given a set of distinct integers, nums, return all possible subsets (the power set). Note: The solut ...
- [LeetCode] Backtracking Template for (Subsets, Permutations, and Combination Sum)
根据issac3 用Java总结了backtracking template, 我用他的方法改成了Python. 以下为template. def backtrack(ans, temp, nums, ...
随机推荐
- 《Structuring Machine Learning Projects》课堂笔记
Lesson 3 Structuring Machine Learning Projects 这篇文章其实是 Coursera 上吴恩达老师的深度学习专业课程的第三门课程的课程笔记. 参考了其他人的笔 ...
- 【JVM学习笔记】类加载器
概述 类加载器用来把类加载到Java虚拟机中.从JDK1.2版本开始,类的加载过程采用父委托机制,这种机制能更好地保证Java平台的安全.在此委托机制中,除了Java虚拟机自带的根类加载器以外,其余的 ...
- Jenkins 有用的API
/quietDown: Put Jenkins in a Quiet mode, in preparation for a restart. In that mode Jenkins don’t st ...
- SQL- 将一张表的数据插入到另一张表,表结构不一致(加条件)
公司业务需要,在对表进行操作的时候将操作人和操作记录记录到日志表里.记录下来以供参考和学习. 首先准备两张测试表:Info以及InfoLog 1.表结构相同的情况下: insert into Info ...
- 【HANA系列】SAP HANA SQL计算两个日期的差值
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[HANA系列]SAP HANA SQL计算两个 ...
- 华为HCNA乱学Round 12:NAT和easy IP
- 笔记本通过命令配置wifi win7系统
查看本子是否支持承载网络 在开始菜单>附件>命令提示符(右键点击:以管理员身份运行) 命令行中输入以下内容,找到[支持的承载网络]这一行,如果为"是"就OK了,表示支持 ...
- 使用PowerShell 在域内远程安装DFS
# 安装DFS 命名空间.DFS 管理工具.DFS 复制# author:lttr <www.cnblogs.com/GoCircle> # date:2019-08-09 # eg. # ...
- Elasticsearch-如何识别一篇文档
ES-识别文档 为了识别同一个索引中的某篇文档,ES使用_uid中的文档类型和ID结合体._uid字段是由_id和_type字段组成,当搜索或者检索文档的时候总是能获得这两项信息. FengZhend ...
- luoguP1352没有上司的舞会(树形DP)
题目链接:https://www.luogu.org/problemnew/show/P1352 题意:给定n个结点,每个结点有一个权值,给n-1条边,n个结点构成一棵树.并且规定一个结点的父结点如果 ...