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(一)的更多相关文章

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

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

  3. Backtracking line search的理解

    使用梯度下降方法求解凸优化问题的时候,会遇到一个问题,选择什么样的梯度下降步长才合适. 假设优化函数为,若每次梯度下降的步长都固定,则可能出现左图所示的情况,无法收敛.若每次步长都很小,则下降速度非常 ...

  4. 重新发现梯度下降法--backtracking line search

    一直以为梯度下降很简单的,结果最近发现我写的一个梯度下降特别慢,后来终于找到原因:step size的选择很关键,有一种叫backtracking line search的梯度下降法就非常高效,该算法 ...

  5. 【原创】回溯线搜索 Backtracking line search

    机器学习中很多数值优化算法都会用到线搜索(line search).线搜索的目的是在搜索方向上找到是目标函数\(f(x)\)最小的点.然而,精确找到最小点比较耗时,由于搜索方向本来就是近似,所以用较小 ...

  6. backtracking问题

    backtracking最基础的问题是Subsets,即给定一个数组,要求返回其所有子集. Given a set of distinct integers, nums, return all pos ...

  7. [LeetCode] 90.Subsets II tag: backtracking

    Given a collection of integers that might contain duplicates, nums, return all possible subsets (the ...

  8. 数组排列组合问题——BACKTRACKING

    BACKTRACKING backtracking(回溯法)是一类递归算法,通常用于解决某类问题:要求找出答案空间中符合某种特定要求的答案,比如eight queens puzzle(将国际象棋的八个 ...

  9. [LeetCode] 78. Subsets tag: backtracking

    Given a set of distinct integers, nums, return all possible subsets (the power set). Note: The solut ...

  10. [LeetCode] Backtracking Template for (Subsets, Permutations, and Combination Sum)

    根据issac3 用Java总结了backtracking template, 我用他的方法改成了Python. 以下为template. def backtrack(ans, temp, nums, ...

随机推荐

  1. 从Windows系统到Linux系统转变的5大要点

    当我在 Algoma  (阿尔格玛)大学开始我现在的工作,一个图书系统管理员,我实在是对我的工作没有什么信心.尽管我在图书信息技术上有十年经验,对于我的第一个任务——协助开发和管理 Evergreen ...

  2. Linux LAMP源码安装

    查看编译参数 # httpd cat /app/httpd24/build/config.nice # mysql cat /app/mysql/docs/INFO_BIN # php php -i ...

  3. java:shiro(认证,赋予角色,授权...)

    1.shiro(权限框架(认证,赋予角色,授权...)): readme.txt(运行机制): 1.从jsp的form中的action属性跳转到springmvc的Handler中(controlle ...

  4. 【MM系列】SAP技巧之更改布局

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[MM系列]SAP技巧之更改布局   前言部分 ...

  5. ssh远程连接的故障排查详解

    排查故障: 1.两个机器之间是否通畅,看物理网络(网线网卡,IP是不是正确) ping ip -t 来检测物理网络是否通畅 通 不通 不通: 1.客户端到服务器端物理链路有问题 网卡 ,IP ,  网 ...

  6. 【Spring Cloud】Spring Cloud使用总结

    项目概要 项目环境信息 IDEA ultimate 2018.3.2 springboot 2.1.7.RELEASE springCloud Greenwich.SR2 Eureka 介绍 基于ne ...

  7. 【C】命令行参数解析——getopt、getopt_long及getopt_long_only

    前言 在linux下学习开源代码Webbench,遇到get_long等函数的用法,一时有点懵,故想深入了解这类命令行解析函数,并记此博文. 1.getopt getopt主要用来处理短命令行选项,例 ...

  8. MySQL_入手<一>增--数据库操作

    创建数据库 create database db_sanguo charset utf8; 切进db_sanguo use db_sanguo 创建英雄 create table t_hero( id ...

  9. 创建node节点的kubeconfig文件

    创建node节点的kubeconfig文件 1.创建TLS Bootstrapping Token export BOOTSTRAP_TOKEN=$(head -c 16 /dev/urandom | ...

  10. Windows7/win10系统安装JDK的环境变量设置javac不是内部命令或外部命令

    ---恢复内容开始--- Windows7/win10系统安装JDK的环境变量设置 Windows7 X64安装“jdk-6u26-windows-x64.exe”后,按照网上的环境变量设置方法设置了 ...