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, ...
随机推荐
- linux 基础 ls cd 目录含义
- 数据库高级数据库学习--上机练习5(Transact-SQL)
上机练习5 启动SQL Server 2008中的 SQL Server Management Studio,恢复数据库ClassDB: 采用Transact-SQL程序设计完成以下练习: . 求1到 ...
- linux常用命令(7)cp命令
cp命令用来复制文件或者目录,是Linux系统中最常用的命令之一.一般情况下,shell会设置一个别名,在命令行下复制文件时,如果目标文件已经存在,就会询问是否覆盖,不管你是否使用-i参数.但是如果是 ...
- Django>ORM字段和参数
Django之ORM字段和参数 字段 常用字段 AutoField 自增int自增列,必须填入参数 primary_key=True. 当model中如果没有自增列,则自动会创建一个列名为id的列 ...
- JS-T
取整函数ceil:向上取整floor:向下取整round:四舍五入 js获取当前页面信息this.location.href JS打印对象 var data = JSON.stringify(res. ...
- Django的一些注意事项
不要使用 Python 或 Django 的组件名命名项目.具体而言,不要使用“django”(与 Django 冲 突)或“test”(与 Python 内置的一个包冲突)这样的名称. 在中文版中, ...
- AI测试——旅程的终点
在2019年6月,我产生了一个想法,即人工智能探索测试AIET(Artificial intelligence exploration test),大概用了一周时间来思考怎么把人工智能应用到测试领域, ...
- PHP7 开启Zend Opcache
PHP7 开启Zend Opcache 作为PHP这10年来最大的版本与性能升级,PHP7在多次的测试中都表现出很夸张的性能提升,然而,为了让它能发挥出最大的性能,需要手动开启PHP自带的opcach ...
- SpreadJS 纯前端表格控件 V12.2 发布更新
用不到100行代码,在前端实现Excel的全部功能 千万前端开发者翘首企盼,SpreadJS V12.2 终发布更新:六大功能特性,带来更多便利,用不到100行代码,在前端实现Excel的全部功能! ...
- Python使用pycharm导入pymysql
file->setting->project->project interperter,双击右侧出现的pip,弹出安装包,搜索pymysql->选择第一个->Instal ...