力扣爆刷第124天之回溯五连刷

news/2024/9/23 2:26:48/

力扣爆刷第124天之回溯五连刷(分割回文、复原IP、子集)

文章目录

      • 力扣爆刷第124天之回溯五连刷(分割回文、复原IP、子集)
      • 一、131. 分割回文串
      • 二、93. 复原 IP 地址
      • 三、78. 子集
      • 四、90. 子集 II
      • 五、91. 非递减子序列

一、131. 分割回文串

题目链接:https://leetcode.cn/problems/palindrome-partitioning/description/
思路:常规组合题,需要起始位置,然后只需要在向下递归的时候判断一下是否是回文,是回文就递归不是就不递归。

class Solution {List<List<String>> resList = new ArrayList<>();List<String> list = new ArrayList<>();public List<List<String>> partition(String s) {backTracking(s, 0);return resList;}void backTracking(String s, int index) {if(index == s.length()) {resList.add(new ArrayList(list));return ;}for(int i = index; i < s.length(); i++) {if(isTrue(s, index, i)) {list.add(s.substring(index, i+1));backTracking(s, i+1);list.remove(list.size()-1);}}}boolean isTrue(String s, int x, int y) {while(x < y) {if(s.charAt(x) != s.charAt(y)) {return false;}x++;y--;}return true;}
}

二、93. 复原 IP 地址

题目链接:https://leetcode.cn/problems/restore-ip-addresses/description/
思路:也是典型的组合问题,和上一题类似,都需要在向下递归的时候进行元素是否需要收集的判断,需要的话才递归。

class Solution {List<String> resList = new ArrayList<>();List<String> list = new ArrayList<>();public List<String> restoreIpAddresses(String s) {backTracking(s, 0);return resList;}void backTracking(String s, int index) {if(list.size() == 4) {if(index == s.length()) {resList.add(String.join(".", list));}return ;}for(int i = index; i < s.length(); i++) {if(list.size() == 3 && i - index > 2) return;String str = isTrue(s, index, i);if(str != null) {list.add(str);backTracking(s, i+1);list.remove(list.size()-1);}}}String isTrue(String s, int x, int y) {if(y - x > 2) return null;if(y - x >= 1 && s.charAt(x) == '0') return null;String ss = s.substring(x, y+1);Integer i = Integer.valueOf(ss);if(i >= 0 && i <= 255) return ss;return null;}
}

三、78. 子集

题目链接:https://leetcode.cn/problems/subsets/description/
思路:常规组合,元素无重,只不过收集节点时,需要收集全部节点,包括非叶子节点和叶子节点。

class Solution {List<List<Integer>> resList = new ArrayList<>();List<Integer> list = new ArrayList<>();public List<List<Integer>> subsets(int[] nums) {backTracking(nums, 0);return resList;}void backTracking(int[] nums, int start) {resList.add(new ArrayList(list));for(int i = start; i < nums.length; i++) {list.add(nums[i]);backTracking(nums, i+1);list.remove(list.size()-1);}}}

四、90. 子集 II

题目链接:https://leetcode.cn/problems/subsets-ii/description/
思路:和上一题类似,不同的地方是集合有重复元素,但好消息是可以排序,这样就可以横向去重,利用相邻元素相同。
相同的地方就是都是全部节点收集。

class Solution {List<List<Integer>> resList = new ArrayList<>();List<Integer> list = new ArrayList<>();public List<List<Integer>> subsetsWithDup(int[] nums) {Arrays.sort(nums);backTracking(nums, 0);return resList;}void backTracking(int[] nums, int start) {resList.add(new ArrayList(list));for(int i = start; i < nums.length; i++) {if(i > start && nums[i] == nums[i-1]) continue;list.add(nums[i]);backTracking(nums, i+1);list.remove(list.size()-1);}}
}

五、91. 非递减子序列

题目链接:https://leetcode.cn/problems/non-decreasing-subsequences/description/
思路:求非递减子序列,不能排序,集合有重复元素,要达到横向去重,需要使用set,每次递归向下都是一个新的set,横向for循环是同一个。

class Solution {List<List<Integer>> resList = new ArrayList<>();List<Integer> list = new ArrayList<>();public List<List<Integer>> findSubsequences(int[] nums) {backTracking(nums, 0);return resList;}void backTracking(int[] nums, int start) {if(list.size() > 1) {resList.add(new ArrayList(list));}Set<Integer> set = new HashSet<>();for(int i = start; i < nums.length; i++) {if(list.size() > 0 && nums[i] < list.get(list.size()-1)) continue;if(set.contains(nums[i])) continue;set.add(nums[i]);list.add(nums[i]);backTracking(nums, i+1);list.remove(list.size()-1); }}
}

http://www.ppmy.cn/news/1431137.html

相关文章

ACM生涯总结

大一时迷恋上了算法竞赛&#xff0c;抓紧一切课余时间进行训练&#xff0c;也顺利了进入了学校的ACM-ICPC集训队。 大二以为能够拿到银牌&#xff0c;但命运和我开了个玩笑&#xff0c;连续两次拿到铜首&#xff08;一次差一名&#xff0c;一次差两名&#xff09;。 大三上的…

Qt Android 无法加载 assets 目录下 lua 校准脚本

问题描述 C 语言使用 fopen 无法打开 assets 目录下的文件。 项目的校准脚本在打包的时候都放在 assets 资源目录下&#xff0c;但是 assets 是压缩包&#xff0c;Android 下虚拟目录&#xff0c;所以 Qt 可以加载 assets 目录下文件&#xff0c;但是 C 语言的 fropen 函数却…

了解边缘计算,在制造行业使用边缘计算。

边缘计算是一种工业元宇宙技术&#xff0c;可以帮助组织实现其数据的全部潜力。 处理公司的所有数据可能具有挑战性&#xff0c;而边缘计算可以帮助公司更快地处理数据。在制造业中&#xff0c;边缘计算可以帮助进行预测性维护和自动驾驶汽车操作等工作。 什么是边缘计算? …

市场投放用户获取方面如何做数据分析

常用数据分析指标 1. 基础指标 下载量: 指通过广告投放带来的下载安装量。 安装率: 指广告点击后下载安装的用户占比。 激活率: 指下载安装后启动应用的用户占比。为了防止假量和刷量&#xff0c;一般会把激活动作定义得更严格更深层一些。比如用户浏览30秒&#xff0c;用户…

力扣(leetcode) 407. 接雨水 II 3D接雨水

力扣(leetcode) 407. 接雨水 II 3D接雨水 给你一个 m x n 的矩阵&#xff0c;其中的值均为非负整数&#xff0c;代表二维高度图每个单元的高度&#xff0c;请计算图中形状最多能接多少体积的雨水。 示例 1: 输入: heightMap [[1,4,3,1,3,2],[3,2,1,3,2,4],[2,3,3,2,3,1]] 输…

Macs Fan Control Pro for Mac:全面优化Mac风扇控制软件

Macs Fan Control Pro for Mac是一款专为苹果电脑用户设计的风扇控制软件&#xff0c;旨在通过精确的风扇速度调节&#xff0c;全面优化Mac的散热性能&#xff0c;确保系统始终运行在最佳状态。 Macs Fan Control Pro for Mac中文版下载 该软件具备实时监控功能&#xff0c;能够…

【WSL报错】执行:wsl --list --online;错误:0x80072ee7

【WSL报错】执行:wsl --list --online&#xff1b;错误:0x80072ee7 问题情况解决方法详细过程 问题情况 C:\Users\17569>wsl --list --online 错误: 0x80072ee7 解决方法 开系统代理&#xff0c;到外网即可修复&#xff01;&#xff01;&#xff01;&#xff01;&#x…

掌控基础设施,加速 DevOps 之旅:IaC 深度解析

在当今的 DevOps 世界中&#xff0c;基础设施即代码&#xff08;IaC&#xff09;是一个非常重要的概念。它在整个行业几乎无处不在&#xff0c;是现代工程角色的绝对关键。 本文将主要包含 IaC 的定义和它的好处&#xff0c;同时将 Walrus 作为最佳实践来进行详细讲解。 什么是…