回溯---java---黑马

server/2024/12/23 8:10:34/

回溯

概念

程序在运行过程中分成了多个阶段

通过某些手段,将数据恢复到某一阶段,称之为回溯

手段包括:方法栈、自定义栈

使用基本数据类型n

java">public class Backtracking{public static void main(String[] args) {rec(1);}public void rec(int n) {if (n == 3) {return ;}System.out.println(n);rec(n + 1);System.out.println(n);}
}

使用引用数据类型

java">public class Backtracking{public static void main(String[] args) {rec(1, new LinkedList<>());}public void rec(int n, LinkedList<String> list) {if (n == 3) {return ;}System.out.println("before" + list);list.add('a');rec(n + 1, list);// list.pop();System.out.println("after" + list);}
}

全排列

leetcode46

java">public class Leetcode46{public List<List<Integer>> permute(int[] nums) {List<List<Integer>> res = new ArrayList<>(); boolean[] visited = new boolean[nums.length];dfs(nums, visited, new LinkedList<>(), res)}public void dfs(int[] nums, boolean[] visited, LinkedList<String> stack, List<List<Integer>> res) {if (stack.size() == nums.length) {res.add(new ArrayList<>(stack));return;}for(int i = 0; i < nums.length; i++) {if (!visited[i]) {stack.push(nums[i]);visited[i] = true;dfs(nums, visited, stack, res);visited[i] = false;stack.pop();}}}
}

当数组中存在重复元素时,需要去掉重复元素的排列,前提是要排序数组

java">public class Leetcode46{public List<List<Integer>> permute(int[] nums) {Arrays.sort(nums);List<List<Integer>> res = new ArrayList<>(); boolean[] visited = new boolean[nums.length];dfs(nums, visited, new LinkedList<>(), res)}public void dfs(int[] nums, boolean[] visited, LinkedList<Integer> stack, List<List<Integer>> res) {if (stack.size() == nums.length) {res.add(new ArrayList<>(stack));return;}for(int i = 0; i < nums.length; i++) {if (i > 0 && nums[i] == nums[i - 1] && !visited[i - 1]) {continue;}if (!visited[i]) {stack.push(nums[i]);visited[i] = true;dfs(nums, visited, stack, res);visited[i] = false;stack.pop();}}}
}

组合

Leetcode77

java">public class Leetcode77{public List<List<Integer>> combine(int n) {List<List<Integer>> res = new ArrayList<>(); dfs(1, n, k, new LinkedList<>(), res);}public void dfs(int start, int n, int k, LinkedList<Integer> stack, List<List<Integer>> res) {if (stack.size() == k) {res.add(new ArrayList<>(stack));return;}for(int i = start, i <= n; i++) {if (k - stack.size() > n - i + 1) {continue;}    stack.push(i);dfs(i + 1, n, k, stack, res);stack.pop(i);}}
}

Leetcode39

java">public class Leetcode39{public List<List<Integer>> combinationSum(int[] nums, int k) {List<List<Integer>> res = new ArrayList<>();dfs(nums, 0, k, new LinkedList<>(), res);return res;}public void dfs(int[] nums, int start, int target, LinkedList<Integer> stack, List<List<Integer>> res) {if (target == 0) {res.add(new ArrayList<>(stack));}for(int i = start; i < nums.length; i++) {if (target < nums[i]) {continue;}stack.push(nums[i]);dfs(nums, i, target - nums[i], stack, res);stack.pop();}}
}

Leetcode40

java">public class Leetcode40{public List<List<Integer>> combinationSum(int[] nums, int k) {List<List<Integer>> res = new ArrayList<>();Arrays.sort(nums);dfs(nums, 0, k, new boolean[], new LinkedList<>(), res);return res;}public void dfs(int[] nums, int start, int target, boolean[] visited, LinkedList<Integer> stack, List<List<Integer>> res) {if (target == 0) {res.add(new ArrayList<>(stack));}for(int i = start; i < nums.length; i++) {if (target < nums[i]) {continue;}if (i > 0 && nums[i] == nums[i - 1] && !visited[i - 1]) {continue;}stack.push(nums[i]);visited[i] = true;dfs(nums, i + 1, target - nums[i], stack, res);visited[i] = false;stack.pop();}}
}

Leetcode216

java">public class Leetcode216{public List<List<Integer>> combinationSum(int k, int target) {List<List<Integer>> res = new ArrayList<>();dfs(1, k, target, new LinkedList<>(), res);return res;}public void dfs(int start, int k, int target, new LinkedList<Integer> stack, List<List<Integer>> res) {if (stack.size() == k && target == 0) {res.add(new ArrayList<>(stack));}for(int i = start, int i <= 9; i++) {if (target < i) {continue;}if (stack.size() == k) {continue;}stack.push(i);dfs(i + 1, k, target - i, stack, res);stack.pop();}}
}

Leetcode51-N皇后

java">public class Leetcode51{public static void main(String[] args) {int n = 4;boolean[] ca = new boolean[n];boolean[] cb = new boolean[2 * n - 1];boolean[] cc = new boolean[2 * n - 1];char[][] board = new char[n][n];List<List<String>> res = new ArrayList<>();for(char[] b : board) {Arrays.fill(b, '.');}dfs(0, n, board, ca, cb, cc, res);return res;}public void dfs(int i, int n, char[][] board, boolean[] ca, boolean[] cb, boolean[] cc, List<List<String>> res) {if (i == n) {List<String> list = new ArrayList<>();for(char[] b : board) {list.add(new String(b));}res.add(list);return;}for(int j = 0; j < n; j++) {if (ca[j] || cb[i + j] || cc[n - 1 - (i - j)]) {continue;}board[i][j] = 'Q';ca[j] = cb[i + j] = cc[n - 1 - (i - j)] = true;dfs(i + 1, n, board, ca, cb, cc, res);ca[j] = cb[i + j] = cc[n - 1 - (i - j)] = false;board[i][j] = '.';}}
}

Leetcode37解数独

java">public class Leetcode37{public void solveSudoku(char[][] board) {int n = board.length;boolean[][] ca = new boolean[n][n];boolean[][] cb = new boolean[n][n];boolean[][] cc = new boolean[n][n];for(int i = 0; i < n; i++) {for(int j = 0; j < n; j++) {char ch = board[i][j];if (ch != '.') {ca[i][ch - '1'] = true;cb[j][ch - '1'] = true;cc[i / 3 * 3 + j / 3][ch - '1'] = true;}}}dfs(0, 0, board, ca, cb, cc);}public static boolean dfs(int i, int j, char[][] board, boolean[][] ca, boolean[][] cb, boolean[][] cc) {while (board[i][j] != '.') {if (++j >= 9) {j = 0;i++;}if (i >= 9) {return true;}}for(int x = 1; x <= 9; x++) {if (ca[i][x - 1] || cb[j][x - 1] || cc[i / 3 * 3 + j / 3][x - 1]) {continue;}board[i][j] = (char) (x + '0');ca[i][x - 1] = cb[j][x - 1] = cc[i / 3 * 3 + j / 3][x - 1] = true;if (dfs(i, j, board, ca, cb, cc)) {return true;}ca[i][x - 1] = cb[j][x - 1] = cc[i / 3 * 3 + j / 3][x - 1] = false;board[i][j] = '.';}return false;}
}

Leetcode167-两数之和

java">public class Leetcode167{public int[] twoSum(int[] nums, int target) {int i = 0;int j = nums.length - 1;while (i < j) {if (nums[i] + nums[j] < target) {i++;} else if (nums[i] + nums[j] > target) {j--;} else {break;}}return new int[]{i + 1, j + 1};}
}
java">public class Leetcode167{public int[] twoSum(int[] nums, int target) {int n = nums.length;for(int i = 0; i < n; i++) {int left = i + 1;int t = target - nums[i];int right = n - 1;while (left <= right) {int m = (left + right) >>> 1;if (t < nums[m]) {j = m - 1;} else if (t > nums[m]) {i = m + 1;} else {return new int[]{i + 1, m + 1};}}}return null;}
}

Leetcode15-三数之和

java">public class Leetcode15{public List<List<Integer>> threeSum(int[] nums, int target) {Arrays.sort(nums);List<List<Integer>>res = new ArrayList<>();int n = nums.length;dfs(3, 0, n - 1, target, new LinkedList<>(), res);return res;}public void dfs(int n, int i, int j, int target, int[] nums, LinkedList<Integer> stack, List<List<Integer>> res) {if (n == 2) {// 两数之和twoSum(i, j, target, nums, stack, res);}for(int k = i; k < j; k++) {if (k > i && nums[k] == nums[k - 1]) {continue;}stack.push(nums[k]);dfs(n - 1, k + 1, j, target - nums[k], nums, stack, res);stack.pop();}}public void twoSum(int i, int j, int target, int[] nums, LinkedList<Integer> stack, List<List<Integer>> res) {while (i < j) {if (nums[i] + nums[j] < target) {i++;} else if (nums[i] + nums[j] > target) {j--;} else {List<Integer> list = new ArrayList<>(stack);list.add(nums[i]);list.add(nums[j]);res.add(list);i++;j--;while (i < j && nums[i] == nums[i - 1]) {i++;}while (i < j && nums[j] == nums[j + 1]) {j--;}}}}
}

Leetcode18-四数之和

java">public class Leetcode15{public List<List<Integer>> threeSum(int[] nums, int target) {Arrays.sort(nums);List<List<Integer>>res = new ArrayList<>();int n = nums.length;dfs(4, 0, n - 1, target, new LinkedList<>(), res);return res;}public void dfs(int n, int i, int j, int target, int[] nums, LinkedList<Integer> stack, List<List<Integer>> res) {if (n == 2) {// 两数之和twoSum(i, j, target, nums, stack, res);}for(int k = i; k < j - (n - 2); k++) {if (k > i && nums[k] == nums[k - 1]) {continue;}stack.push(nums[k]);dfs(n - 1, k + 1, j, target - nums[k], nums, stack, res);stack.pop();}}public void twoSum(int i, int j, int target, int[] nums, LinkedList<Integer> stack, List<List<Integer>> res) {while (i < j) {if (nums[i] + nums[j] < target) {i++;} else if (nums[i] + nums[j] > target) {j--;} else {List<Integer> list = new ArrayList<>(stack);list.add(nums[i]);list.add(nums[j]);res.add(list);i++;j--;while (i < j && nums[i] == nums[i - 1]) {i++;}while (i < j && nums[j] == nums[j + 1]) {j--;}}}}
}

Leetcode11

java">public class Leetcode11{public int MostWaterLeetcode11(int[] height) {int i = 0; int j = height.length - 1;int max = 0;while (i < j) {if (nums[i] <= nums[j]) {i++;} else {j--;}max = Integer.max(max, Integer.min(nums[i], nums[j]) * (j - i + 1))}retu}}

http://www.ppmy.cn/server/152441.html

相关文章

半导体数据分析(二):徒手玩转STDF格式文件 -- 码农切入半导体系列

一、概述 在上一篇文章中&#xff0c;我们一起学习了STDF格式的文件&#xff0c;知道了这是半导体测试数据的标准格式文件。也解释了为什么码农掌握了STDF文件之后&#xff0c;好比掌握了切入半导体行业的金钥匙。 从今天开始&#xff0c;我们一起来一步步地学习如何解构、熟…

从零用java实现 小红书 springboot vue uniapp (5)购物页聊天页

前言 移动端演示 http://8.146.211.120:8081/#/ 前面的文章我们基本完成了个人中心页开发 今天我们具体的去进行实现购物页 聊天页 并且分享我开发时遇到的问题 首先先看效果 商品页 商品数据先用笔记数据 我们对布局整体规划一下 搜索组件 搜索组件是fiexd布局一直在页面…

AI的进阶之路:从机器学习到深度学习的演变(三)

&#xff08;承接上集&#xff1a;AI的进阶之路&#xff1a;从机器学习到深度学习的演变&#xff08;二&#xff09;&#xff09; 四、深度学习&#xff08;DL&#xff09;&#xff1a;机器学习的革命性突破 深度学习&#xff08;DL&#xff09;作为机器学习的一个重要分支&am…

电商新品发布自动化:RPA 确保信息一致性与及时性【rap.top】

一、教学目标 让学员了解电商新品发布过程中的挑战以及 RPA 的概念和优势。掌握 RPA 在电商新品发布中确保信息一致性与及时性的方法和流程。培养学员运用 RPA 解决实际问题的能力。 二、教学重难点 重点 RPA 在电商新品发布中的应用场景。实现信息一致性与及时性的具体策略…

网络安全概论——网络安全基础

一、网络安全引言 信息安全的四个属性&#xff08;信息安全的基本目标 &#xff09; 保密性:信息不会被泄露给非授权用户完整性&#xff1a;保证数据的一致性可用性&#xff1a;合法用户不会被拒绝服务合法使用&#xff1a;不会被非授权用户或以非授权的方式使用 二、网络安…

VSCode 启用免费 Copilot

升级VSCode到 1.96版本&#xff0c;就可以使用每个月2000次免费额度了&#xff0c;按照工作日每天近80次免费额度&#xff0c;满足基本需求。前两天一直比较繁忙&#xff0c;今天周六有时间正好体验一下。 引导插件安装GitHub Copilot - Visual Studio Marketplace Extension f…

豆包MarsCode:小U的数字插入问题

问题描述 问题分析 问题的核心是找到将数字 b 插入到数字 a 的某个位置后&#xff0c;使形成的数字尽可能大。需要仔细分析以下几个要点&#xff1a; 1. 分析数字的特性 输入的两个数字&#xff1a; a 是一个正整数&#xff08;例如 76543&#xff09;。b 是一个非负整数&am…

条款34 考虑lambda而非std::bind

一、lambda比std::bind可读性更高 lambda与正常写一个函数其实没有什么区别&#xff0c;但是std::bind的传入参数是理科调用的 auto setSoundL[](int x) { setAlarm(steady_clock::now());}; // auto setSoundBstd::bind(setAlarm,steady_clock::now(),_1); //因为是立刻执行…