DAY28| 93. 复原IP地址 ,79.子集 ,90.子集II

devtools/2024/9/22 21:23:48/

文章目录

    • 93.复原IP地址
    • 78.子集
    • 90.子集II

93.复原IP地址

文字讲解:复原IP地址

视频讲解:复原IP地址

**状态:**此题调试了几次ok,与昨天的分割回文子串相比,就是在判断终止条件处需要处理;

思路:

代码:

java">class Solution {List<String> result = new ArrayList<>();LinkedList<String> tempList = new LinkedList<>();public List<String> restoreIpAddresses(String s) {backTracking(0, s);return result;}public void backTracking(Integer startIndex, String s) {if (tempList.size() == 3) {if (validIpParam(s.substring(startIndex, s.length()))) {tempList.offer(s.substring(startIndex, s.length()));result.add(getIp());tempList.pollLast();}return;}for (int i = startIndex; i < s.length() && tempList.size()<=4; i++) {if (validIpParam(s.substring(startIndex, i+1))) {tempList.offer(s.substring(startIndex, i+1));} else {continue;}backTracking(i+1, s);tempList.pollLast();}}public boolean validIpParam(String s) {if (s == null || s.length()==0) {return false;}if (s.length()>=2 && s.charAt(0)=='0') {return false;}if (s.length()>3) {return false;}Integer num = Integer.valueOf(s);if (num < 0 || num > 255) {return false;}return true;}public String getIp() {StringBuilder resultStr = new StringBuilder();for (int i = 0; i < tempList.size(); i++) {if (i==tempList.size()-1) {resultStr.append(tempList.get(i));} else {resultStr.append(tempList.get(i)).append(".");}}return resultStr.toString();}
}

78.子集

文字讲解:子集

视频讲解:子集

状态:这一题的关键在于收集元素的位置,理解了回溯算法中的树形结构和理论知识,这题可以想到在for循环中收集元素即可

思路:

代码:

java">class Solution {List<List<Integer>> result = new ArrayList<>();LinkedList<Integer> tempList = new LinkedList<>();public List<List<Integer>> subsets(int[] nums) {backTracking(nums, 0);result.add(new ArrayList<>());return result;}public void backTracking(int[] nums, int index) {if (index>=nums.length) {return;}for (int i = index; i < nums.length; i++) {tempList.add(nums[i]);//收集元素result.add(new ArrayList<>(tempList));backTracking(nums, i+1);tempList.pollLast();}}
}

90.子集II

文字讲解:子集II

视频讲解:子集II

状态:这题秒了

思路:

代码:

java">class Solution {List<List<Integer>> result = new ArrayList<>();LinkedList<Integer> tempList = new LinkedList<>();public List<List<Integer>> subsetsWithDup(int[] nums) {result.add(tempList);//对数组先进行排序Arrays.sort(nums);backTracking(nums, 0);return result;}public void backTracking(int[] nums, int startIndex) {if (startIndex>=nums.length) {return;}for (int i = startIndex; i < nums.length; i++) {if (i>startIndex&&nums[i]==nums[i-1]) {continue;}tempList.offer(nums[i]);result.add(new ArrayList<>(tempList));backTracking(nums, i+1);tempList.pollLast();}}
}

http://www.ppmy.cn/devtools/4097.html

相关文章

高频前端面试题汇总之前端性能优化篇

一、CDN 1. CDN的概念 CDN&#xff08;Content Delivery Network&#xff0c;内容分发网络&#xff09;是指一种通过互联网互相连接的电脑网络系统&#xff0c;利用最靠近每位用户的服务器&#xff0c;更快、更可靠地将音乐、图片、视频、应用程序及其他文件发送给用户&#…

分类算法——模型选择与调优(三)

交叉验证 交叉验证&#xff1a;将拿到的训练数据&#xff0c;分为训练和验证集。以下图为例&#xff1a;将数据分成4份&#xff0c;其中 一份作为验证集。然后经过4次&#xff08;组&#xff09;的测试&#xff0c;每次都更换不同的验证集。即得到4组模型的 结果&#xff0c;取…

antDesign Form表单校验(react)

<script><Form name"basic" ref{formRef} onFinish{onFinish}><Form.Itemlabel校验name"check"rules{[// 校验必填{required: true,message: 请输入&#xff01;},// 校验输入字符数限制{validator: (_, value) >value && value…

分类网络总结

欢迎大家订阅我的专栏一起学习共同进步&#xff0c;主要针对25届应届毕业生 祝大家早日拿到offer&#xff01; lets go http://t.csdnimg.cn/dfcH3 目录 4. 经典分类网络与发展 4.1 AlexNet 4.2 VGGNet 4.3 GoogLeNet Inception 4.4 ResNet 4.5 DenseNet 4.6 MobileN…

监督算法建模前数据质量检查

一、定义缺失值检测函数 def missing_values_table(df):# 总的缺失值mis_val df.isnull().sum()# 缺失值占比mis_val_percent 100 * df.isnull().sum() / len(df)# 将上述值合并成表mis_val_table pd.concat([mis_val, mis_val_percent], axis1)# 重命名列名mis_val_table_…

Matlab三维空间任意位置绘制二维强度图

三维空间任意位置绘制二维强度图, 上述使matlab代码,给出了U_slice123三个切片信息,以及一个三维等值面图,如何实现下图效果? % 你的原始代码 N = 100; c = 3e+8; xbound = 400e-6; tbound = 1.5e-12; ybound = 400e-6; w = 1; t = linspace(tbound, -tbound, N); x =…

BOOT和UBOOT区别与联系

一、定义 1.1 Boot&#xff08;启动&#xff09; 在计算机和嵌入式系统的基本概念中&#xff0c;“boot”是指启动过程&#xff0c;这是一个系统从加电开始直至进入操作系统运行状态的过程。在嵌入式系统中&#xff0c;这个过程通常包括初始化硬件、加载并执行引导加载…

一个开源跨平台嵌入式USB设备协议:TinyUSB

概述 TinyUSB 是一个用于嵌入式系统的开源跨平台 USB 主机/设备堆栈&#xff0c;设计为内存安全&#xff0c;无需动态分配&#xff0c;线程安全&#xff0c;所有中断事件都被推迟&#xff0c;然后在非 ISR 任务函数中处理。查看在线文档以获取更多详细信息。 源码链接&#xff…