全排列(046)

class Solution {
List<List<Integer>> res = new ArrayList<>();
public List<List<Integer>> permute(int[] nums) {
int n = nums.length;
List<Integer> path = new ArrayList<>(n);
for (int num : nums){
path.add(num);
} backTrack(0, path, n);
return res;
} private void backTrack(int idx, List<Integer> path, int len){
if (idx == len-1){
res.add(new ArrayList(path));
return;
} for (int i = idx; i < len; i++){
Collections.swap(path, idx, i);
backTrack(idx+1, path, len);
Collections.swap(path, idx, i);
}
}
}
  • 分析

根据排列组合, 我们可以得到 答案的数量为 nums.length()的阶乘

假设 n = nums.length =3

对于第一个数的选择有三种可能 ∗3

对于第二个数的选择有两种可能 ∗2(两个数未选)

对于第三个数的选择有一种可能 ∗1(一个数未选)

有以下关键代码

for (int i = idx; i < len; i++){
Collections.swap(path, idx, i);
backTrack(idx+1, path, len);
Collections.swap(path, idx, i);
}

子集(078)

class Solution {
List<List<Integer>> res = new ArrayList<>();
public List<List<Integer>> subsets(int[] nums) {
int n = nums.length;
List<Integer> path = new ArrayList<>();
backTrace(0, path, nums, n);
return res;
} private void backTrace(int idx, List<Integer> path, int[] nums, int len){
if (idx == len){
res.add(new ArrayList(path));
return;
}
path.add(nums[idx]);
backTrace(idx+1, path, nums, len);
path.remove(path.size()-1);
backTrace(idx+1, path, nums, len);
}
}
  • 分析

标准的背包问题, 选或不选

电话号码的字母组合(017)

class Solution {
String[] mapping = new String[] { "", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz" };
List<String> res = new ArrayList<>();
public List<String> letterCombinations(String digits) {
int n = digits.length();
if (!digits.isEmpty()) backTrace(0, digits, new StringBuilder(), n);
return res;
} private void backTrace(int idx, String digits, StringBuilder builder, int len){
if (idx == len){
res.add(builder.toString());
return;
} char c = digits.charAt(idx);
c -= '0';
for (char d : mapping[c].toCharArray()){
builder.append(d);
backTrace(idx+1, digits, builder, len);
builder.deleteCharAt(builder.length()-1);
}
}
}
  • 分析

标准简单回溯

组合总和(039)

class Solution {
List<List<Integer>> res = new ArrayList<>();
public List<List<Integer>> combinationSum(int[] candidates, int target) {
Arrays.sort(candidates);
List<Integer> path = new ArrayList<>();
backTrace(0, target, path, candidates);
return res;
}
private void backTrace(int idx, int target, List<Integer> path, int[] candidates){
if (target == 0){
res.add(new ArrayList(path));
return;
} if (idx == candidates.length || target < candidates[idx]) return; backTrace(idx+1, target, path, candidates);
path.add(candidates[idx]);
backTrace(idx, target - candidates[idx], path, candidates);
path.remove(path.size()-1);
}
}
  • 分析

我愿称他为原地背包, 也不知道有没有这种说法

括号生成(022)

class Solution {
List<String> res = new ArrayList<>();
public List<String> generateParenthesis(int n) {
StringBuilder builder = new StringBuilder();
backTrace(n, n, builder);
return res;
}
private void backTrace(int lef, int rig, StringBuilder builder){
if (lef > rig || lef < 0 || rig < 0) return;
if (lef == 0 && rig == 0){
res.add(builder.toString());
return;
} backTrace(lef-1, rig, builder.append('('));
builder.deleteCharAt(builder.length()-1);
backTrace(lef, rig-1, builder.append(')'));
builder.deleteCharAt(builder.length()-1);
}
}
  • 分析

标准回溯 + 括号 →<右括号数量不能大于左括号>的限制条件

hot100之回溯上的更多相关文章

  1. prolog --寻找neni (2)

    混合查询 我们可以把简单的查询连接起来,组成复杂的查询. ?- location(X,kitchen),edible(X). 简单查询只有一个目标,而混合查询可以把这些目标连接起来.从而进行较为复杂的 ...

  2. this prototype 闭包 总结

    this对象 整理下思路: 一般用到this中的情景: 1.构造方法中 function A(){ this.name="yinshen"; } var a=new A(); co ...

  3. leetcode[91] Subsets II

    给定一个数组,返回所有的非重复的可能.例如给定 If S = [1,2,2], a solution is: [ [2], [1], [1,2,2], [2,2], [1,2], [] ] 其实这题类 ...

  4. jquery的2.0.3版本源码系列(3):96行-283行,给JQ对象,添加一些方法和属性

    jquery是面向对象的程序,面向对象就离不开方法和属性. 方法的简化 jQuery.fn=jQuery.prototype={ jquery: 版本 constructor: 修正指向问题 init ...

  5. 胡小兔的OI日志3 完结版

    胡小兔的 OI 日志 3 (2017.9.1 ~ 2017.10.11) 标签: 日记 查看最新 2017-09-02 51nod 1378 夹克老爷的愤怒 | 树形DP 夹克老爷逢三抽一之后,由于采 ...

  6. UVa11212,Editing a Book

    正如书上所说,本题需要用IDA*算法求解 启发函数是3d+h>3maxd(d为当前操作步骤数,h为当前逆序对数,maxd为当前枚举的最大步骤数) 可见迭代递归的核心思想是枚举ans去dfs是否可 ...

  7. C# 数独算法——LINQ+委托

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Sing ...

  8. 剑指offer(65)矩阵中的路径

    题目描述 请设计一个函数,用来判断在一个矩阵中是否存在一条包含某字符串所有字符的路径.路径可以从矩阵中的任意一个格子开始,每一步可以在矩阵中向左,向右,向上,向下移动一个格子.如果一条路径经过了矩阵中 ...

  9. 数据结构——串(KMP)

    空串:长度为0的串 空格串:由一个或多个空格组成的串 串常用的3种机内表示方法: 定长顺序存储表示: 用一组地址连续的存储单元存储串的字符序列,每一个串变量都有一个固定长度的存储区,可用定长数组来描述 ...

  10. 剑指offer(27)字符串的排列

    题目描述 输入一个字符串,按字典序打印出该字符串中字符的所有排列.例如输入字符串abc,则打印出由字符a,b,c所能排列出来的所有字符串abc,acb,bac,bca,cab和cba. 输入描述:输入 ...

随机推荐

  1. npm publish

    # 登录到 npm > npm login Username:[your username] Password:[******] Email:(this IS public):[youre em ...

  2. FastAPI 核心安全功能与模板渲染的完整示:登录、CSRF、JWT、会话、认证和缓存功能

    以下是一个整合 FastAPI 核心安全功能与模板渲染的完整示例,基于多个技术文档的最佳实践,包含登录.CSRF.JWT.会话.认证和缓存功能: from datetime import dateti ...

  3. 使用XML的方式编写:@Aspect运用

    例子. 接口 public interface Calculator { // 加 public int add(int i, int j); // 减 public int sub(int i, i ...

  4. 🎀CSDN-自定义公众号卡片

    简介 在CSDN新增自定义模块,创建自己的公众号卡片用于展示. 效果展示 公众号卡片 动态效果 鼠标移入前为公众号指引页 鼠标移入后显示公众号二维码 切换动画为动态反转 首页效果 前提 CSDN需开通 ...

  5. 🎀隧道代理ip使用

    简介 隧道代理(Tunnel Proxy)是一种特殊的代理服务,它的工作方式是在客户端与远程服务器之间建立一条"隧道".这种技术常被用来绕过网络限制或提高网络安全性. 主要功能 I ...

  6. Web前端入门第 35 问:CSS 细说 flex 弹性盒子布局(多图)

    flex 作为现代布局方案中最常用的手段,有必要拉出来细说. flex 相关的 CSS 属性 容器(父元素)相关的 CSS 属性 以下的 CSS 属性,在 flex 布局中需喂给父元素,设置 flex ...

  7. Cursor入门教程-JetBrains过度向

    Cursor使用笔记 前置:之前博主使用的是JetBrains的IDE,VSCode使用比较少,所以会尽量朝着JetBrains的使用习惯及样式去调整. 一.设置语言为中文 如果刚上手Cursor,那 ...

  8. web自动化:webdriver常用api

    一.获取操作 1.get('url'):访问指定的url webdriver.get(String url); 2.Getcurrenturl():获取当前页面url webDriver.getCur ...

  9. Argo CD

    目录 一.什么是 Argo CD 二.为什么选择 Argo CD 三.Argo CD 架构 1.API服务器 2.存储库服务器 3.应用程序控制器 四.Argo CD 的使用 1.要求 2.安装 Ar ...

  10. 话说神奇的content="IE=edge,chrome=1"的meta标签内容

    这是个是IE8的专用标记,用来指定IE8浏览器去模拟某个特定版本的IE浏览器的渲染方式(比如人见人烦的IE6),以此来解决部分兼容问题,例如模拟IE7的具体方式如下: < meta http-e ...