Week1题目重刷
- 今天把week1的题目都重新刷了一遍,明天开始week2的内容~
704.二分查找
class Solution {public int search(int[] nums, int target) {int l = 0, r = nums.length - 1, m;while (l <= r) {m = (l + r) >>> 1;if (nums[m] < target) {l = m + 1;} else if (nums[m] > target) {r = m - 1;} else {return m;}}return -1;}
}
class Solution {public int search(int[] nums, int target) {int l = 0, r = nums.length, m;while (l < r) {m = (l + r) >>> 1;if (nums[m] < target) {l = m + 1;} else if (nums[m] > target) {r = m;} else {return m;}}return -1;}
}
35.搜索插入位置
class Solution {public int searchInsert(int[] nums, int target) {int l = 0, r = nums.length - 1, m;while (l <= r) {m = (l + r) >>> 1;if (nums[m] < target) {l = m + 1;} else if (nums[m] > target) {r = m - 1;} else {return m;}}return l;}
}
class Solution {public int searchInsert(int[] nums, int target) {int l = 0, r = nums.length, m;while (l < r) {m = (l + r) >>> 1;if (nums[m] < target) {l = m + 1;} else if (nums[m] > target) {r = m;} else {return m;}}return l;}
}
27.移除元素
class Solution {public int removeElement(int[] nums, int val) {int s = 0;for (int f = 0; f < nums.length; f++) {if (nums[f] != val) {nums[s] = nums[f];s++;}}return s;}
}
26. 删除有序数组中的重复项
class Solution {public int removeDuplicates(int[] nums) {if (nums.length == 1) return 1;int s = 0;for (int f = 1; f < nums.length; f++) {if (nums[f] != nums[s]) {s++;nums[s] = nums[f];}}return ++s;}
}
283. 移动零
class Solution {public void moveZeroes(int[] nums) {int s = 0;for (int f = 0; f < nums.length; f++) {if (nums[f] != 0) {nums[s] = nums[f];s++;}}while (s < nums.length) {nums[s] = 0;s++;}}
}class Solution {public void moveZeroes(int[] nums) {int s = 0;for (int f = 0; f < nums.length; f++) {if (nums[f] != 0) {int t = nums[f];nums[f] = nums[s];nums[s] = t;s++;}}}
}
844. 比较含退格的字符串
class Solution {public boolean backspaceCompare(String s, String t) {return removeBackspace(s).equals(removeBackspace(t));}private String removeBackspace(String str) {Deque<Character> quene = new ArrayDeque();for (char c : str.toCharArray()) {if (c != '#') {quene.addFirst(c);} else {if (!quene.isEmpty()) quene.removeFirst();}}String res = new String();while (!quene.isEmpty()) {res += quene.removeLast();}return res;}
}class Solution {public boolean backspaceCompare(String s, String t) {int i = s.length() - 1, j = t.length() - 1, skipS = 0, skipT = 0;while (i >= 0 || j >= 0) { // 只有两个字符串都遍历结束,大循环才结束,长度不一样内部处理while (i >= 0) { // 对s进行循环if (s.charAt(i) == '#') { // 找到#,把skipS + 1skipS++;i--;}else if (skipS > 0) { // 不是# 但skipS > 0 这个字符应该被删除skipS--;i--;}else break; // 找到了一个要比较的字符} // 这个循环结束,要么找到了要比较的字符,要么 i < 0while (j >= 0) {if (t.charAt(j) == '#') {skipT++;j--;}else if (skipT > 0) {skipT--;j--;}else break;} // j 也一样if (i < 0 && j < 0) return true; // 如果都小于零,说明都变成了空串。返回trueif (i < 0 || j < 0) return false; // 有一个大于零,说明长度不一样,返回falseif (s.charAt(i) != t.charAt(j)) return false; // 都大于零,比较一下这两个字符i--; // 别忘了比较之后移动位置j--;}return true; // 大循环结束,说明两个字符串都遍历完了,长度一样,对应字符一样,返回true}
}
977.有序数组的平方
class Solution {public int[] sortedSquares(int[] nums) {int l = 0, r = nums.length - 1;int[] res = new int[nums.length];for (int i = nums.length - 1; i >= 0; i--) {if (nums[l] * nums[l] > nums[r] * nums[r]) {res[i] = nums[l] * nums[l];l++;} else {res[i] = nums[r] * nums[r];r--;}}return res;}
}
209. 长度最小的子数组
class Solution {public int minSubArrayLen(int target, int[] nums) {int sum = 0, j = 0, res = Integer.MAX_VALUE;for (int i = 0; i < nums.length; i++) {sum += nums[i];while (sum >= target) {res = Math.min(res, i - j + 1);sum -= nums[j];j++;}}return res == Integer.MAX_VALUE ? 0 : res;}
}
904. 水果成篮
class Solution {public int totalFruit(int[] fruits) {int left = 0, res = 0;Map<Integer, Integer> map = new HashMap(); // 哈希表,记录元素出现次数for (int right = 0; right < fruits.length; right++) { // 快指针遍历数组map.put(fruits[right], map.getOrDefault(fruits[right], 0) + 1); // 每遍历一个元素,把它在map中的v+1while (map.size() > 2) { // 一旦size>2 说明种类超过了两个,应该开始从左边开始删除元素map.put(fruits[left], map.get(fruits[left]) - 1); // 从左边开始删除元素,这里一定能get到if (map.get(fruits[left]) == 0) { // 因为一旦元素v为0,就removemap.remove(fruits[left]);}left++; // 删除完别忘了移动慢指针}res = Math.max(res, right - left + 1); // 外层循环每进行一次,都要更新一下res}return res; // 最后返回res}
}
76. 最小覆盖子串
class Solution {public String minWindow(String s, String t) {int left = 0, res = Integer.MAX_VALUE, start = 0;int[] countS = new int[128]; // 创建两个数组作为哈希表int[] countT = new int[128];for (char c : t.toCharArray()) {countT[c]++; // 遍历T数组,得到哈希数组}for (int right = 0; right < s.length(); right++) {countS[s.charAt(right)]++; // 快指针走一步,数组更新一次while (check(countS, countT)) { // 一旦check成功,说明s这部分包含了t,可能要更新res,进入内层循环if (right - left + 1 < res) {res = right - left + 1;start = left; // 更新的时候记录start位置}countS[s.charAt(left)]--; // 更新完left指针移动left++;}}return res == Integer.MAX_VALUE ? "" : s.substring(start, start + res);}// check函数的逻辑,一旦发现countT某个位置元素比S大,就返回false,要么比你多要么你没有private boolean check(int[] countS, int[] countT) {for (int i = 0; i < 128; i++) {if (countS[i] < countT[i]) {return false;}}return true;}
}
59.螺旋矩阵II
class Solution {public int[][] generateMatrix(int n) {int l = 0, r = n - 1, b = 0, t = n - 1, num = 1;int[][] res = new int[n][n];while (num <= n * n) {for (int i = l; i <= r; i++) {res[b][i] = num++;}b++;for (int i = b; i <= t; i++) {res[i][r] = num++;}r--;for (int i = r; i >= l; i--) {res[t][i] = num++;}t--;for (int i = t; i >= b; i--) {res[i][l] = num++;}l++;}return res;}
}
203. 移除链表元素
class Solution {public ListNode removeElements(ListNode head, int val) {if (head == null) return null;ListNode dummy = new ListNode(-1, head);ListNode pre = dummy;while (pre.next != null) {if (pre.next.val == val) {pre.next = pre.next.next;} else {pre = pre.next;}}return dummy.next;}
}
class Solution {public ListNode removeElements(ListNode head, int val) {if (head == null) return null;head.next = removeElements(head.next, val);if (head.val == val) head = head.next;return head;}
}
707.设计链表
class MyLinkedList {private ListNode head;private int size;public MyLinkedList() {head = new ListNode(-1);size = 0;}public int get(int index) {if (index < 0 || index >= size) return -1;ListNode cur = head;while (index-- >= 0) {cur = cur.next;}return cur.val;}public void addAtHead(int val) {addAtIndex(0, val);}public void addAtTail(int val) {addAtIndex(size, val);}public void addAtIndex(int index, int val) {if (index > size) return;ListNode pre = head;while (index-- > 0) {pre = pre.next;}ListNode node = new ListNode(val, pre.next);pre.next = node;size++;}public void deleteAtIndex(int index) {if (index < 0 || index >= size) return;ListNode pre = head;while (index-- > 0) {pre = pre.next;}pre.next = pre.next.next;size--;}
}class ListNode {int val;ListNode next;public ListNode(){};public ListNode(int val, ListNode next) {this.val = val;this.next = next;}public ListNode(int val) {this.val = val;}
}/*** Your MyLinkedList object will be instantiated and called as such:* MyLinkedList obj = new MyLinkedList();* int param_1 = obj.get(index);* obj.addAtHead(val);* obj.addAtTail(val);* obj.addAtIndex(index,val);* obj.deleteAtIndex(index);*/
206.反转链表
class Solution {public ListNode reverseList(ListNode head) {if (head == null) return head;ListNode pre = head;ListNode cur = pre.next;ListNode temp;while (cur != null) {temp = cur.next;cur.next = pre;if (pre == head) pre.next = null;pre = cur;cur = temp;}return pre;}
}
class Solution {public ListNode reverseList(ListNode head) {if (head == null || head.next == null) return head;ListNode newHead = reverseList(head.next);head.next.next = head;head.next = null;return newHead;}
}
24.两两交换链表中的节点
class Solution {public ListNode swapPairs(ListNode head) {if (head == null || head.next == null) return head;ListNode reHead = swapPairs(head.next.next);ListNode res = head.next;head.next.next = head;head.next = reHead;return res;}
}
class Solution {public ListNode swapPairs(ListNode head) {if (head == null || head.next == null) return head;ListNode dummy = new ListNode(-1, head);ListNode pre = dummy;ListNode cur = head;ListNode temp;while (cur != null && cur.next != null) {temp = cur.next;pre.next = temp;cur.next = temp.next;temp.next = cur;pre = cur;cur = pre.next;}return dummy.next;}
}
19.删除链表的倒数第N个节点
class Solution {public ListNode removeNthFromEnd(ListNode head, int n) {ListNode dummy = new ListNode(-1, head);ListNode temp = dummy;ListNode s = dummy;while (n-- > 0) {temp = temp.next;}while (temp.next != null) {temp = temp.next;s = s.next;}s.next = s.next.next;return dummy.next;}
}
面试题 02.07. 链表相交
public class Solution {public ListNode getIntersectionNode(ListNode headA, ListNode headB) {ListNode tA = headA;ListNode tB = headB;while (tA != tB) {tA = tA == null ? headB : tA.next;tB = tB == null ? headA : tB.next;}return tA;}
}
142.环形链表Ⅱ
public class Solution {public ListNode detectCycle(ListNode head) {ListNode f = head, s = head;while (f != null && f.next != null) {f = f.next.next;s = s.next;if (f == s) {f = head;while (f != s) {f = f.next;s = s.next;}return f;}}return null;}
}
相关文章:
Week1题目重刷
今天把week1的题目都重新刷了一遍,明天开始week2的内容~ 704.二分查找 class Solution {public int search(int[] nums, int target) {int l 0, r nums.length - 1, m;while (l < r) {m (l r) >>> 1;if (nums[m] < target) {l m 1;} else if…...
考研数据结构:第七章 查找
文章目录 一、查找的基本概念二、顺序查找和折半查找2.1顺序查找2.3折半查找2.3.1算法思想2.3.2代码实现2.3.3查找效率分析2.3.4折半查找判定树的构造2.3.5折半查找效率2.3.6小结 2.4分块查找 三、树形查找3.1二叉排序树3.1.1二叉排序树定义3.1.2查找操作3.1.3插入操作3.1.4二叉…...
【Linux进程篇】环境变量
【Linux进程篇】环境变量 目录 【Linux进程篇】环境变量基本概念常见环境变量查看环境变量方法测试PATH测试HOME测试SHELL和环境变量相关的命令环境变量的组织方式通过代码如何获取环境变量命令行参数命令行第三个参数通过第三方变量environ获取 本地变量通过系统调用获取或设置…...
【软件测试】Linux环境下Docker搭建+Docker搭建MySQL服务(详细)
目录:导读 前言 一、Python编程入门到精通二、接口自动化项目实战三、Web自动化项目实战四、App自动化项目实战五、一线大厂简历六、测试开发DevOps体系七、常用自动化测试工具八、JMeter性能测试九、总结(尾部小惊喜) 前言 Linux之docker搭…...
去了字节跳动,才知道年薪40W的测试有这么多?
今年大环境不好,内卷的厉害,薪资待遇好的工作机会更是难得。最近脉脉职言区有一条讨论火了: 哪家互联网公司薪资最‘厉害’? 下面的评论多为字节跳动,还炸出了很多年薪40W的测试工程师 我只想问一句,现在的…...
linux0.95(VFS重点)源码通俗解读(施工中)
文件系统在磁盘中的体现 下面是磁盘的内容,其中i节点就是一个inode数组,逻辑块就是数据块可用于存放数据 操作系统通过将磁盘数据读入到内存中指定的缓冲区块来与磁盘交互,对内存中的缓冲区块修改后写回磁盘。 进程(task_struct * task[N…...
mac ssh连接另一台window虚拟机vm
vmware配置端口映射 编辑(E) > 虚拟网络编辑器(N)... > NAT设置(S)... window防火墙,入站规则添加5555端口 控制面板 > 系统和安全 > Windows 防火墙>高级设置>入站规则>新建规则... tips windows查看端口命令:netstat -ano | f…...
使用Python解析通达信本地lday数据结构
通达信软件中的vipdoc是一个存储股票行情数据的文件夹。在通达信软件的安装目录下,可以找到一个名为vipdoc的文件夹,里面存放着各个股票的分时、日线、周线、月线等行情数据文件。这些数据文件可以用于自定义分析和回测股票的走势和交易策略,…...
【Mysql】修改definer
修改definer 本文介绍如何修改MySQL中的function、procedure、event、view和trigger的definer 修改function、procedure的definer 首先,我们需要登录MySQL命令行界面,然后执行以下命令: select definer from mysql.proc;这个命令会列出所…...
图片预览插件vue-photo-preview的使用
移动端项目中需要图片预览的功能,但本身使用mintui,vantui中虽然也有,但是为了一个组件安装这个有点儿多余,就选用了vue-photo-preview插件实现(其实偷懒也不想自己写)。 1、安装 npm i vue-photo-preview…...
最强自动化测试框架Playwright(20)- iframe
一个页面可以附加一个或多个 Frame 对象。每个页面都有一个主框架,并且假定页面级交互(如)在主框架中运行。click frame_locator 使用 iframe 时,可以创建一个框架定位器,该定位器将进入 iframe 并允许选择该 iframe…...
leetcode 516. 最长回文子序列(JAVA)题解
题目链接https://leetcode.cn/problems/longest-palindromic-subsequence/description/?utm_sourceLCUS&utm_mediumip_redirect&utm_campaigntransfer2china 目录 题目描述: 暴力递归: 动态规划: 题目描述: 给你一个…...
在Java中操作Redis(详细-->从环境配置到代码实现)
在Java中操作Redis 文章目录 在Java中操作Redis1、介绍2、Jedis3、Spring Data Redis3.1、对String的操作3.2、对哈希类型数据的操作3.3、对list的操作3.4、对set类型的操作3.5、对 ZSet类型的数据(有序集合)3.6、通用类型的操作 1、介绍 Redis 的Java客…...
分布式作业调度框架——ElasticJob
1、简介 ElasticJob 是面向互联网生态和海量任务的分布式调度解决方案,由两个相互独立的子项目 ElasticJob-Lite 和 ElasticJob-Cloud 组成。 它通过弹性调度、资源管控、以及作业治理的功能,打造一个适用于互联网场景的分布式调度解决方案,…...
react如何实现数据渲染
React数据渲染是指将组件中的数据映射到页面上,以展示出来。在React中,数据渲染通常是通过JSX和组件的state或props完成的。 JSX是一个类似HTML的语法,可以在其中嵌入JavaScript表达式。在JSX中,可以使用{}包裹JavaScript表达式&…...
在Java中如何使用List集合实现分页,以及模糊查询后分页
物理分页工具类 package com.yutu.garden.utils;import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors;/*** ClassName: PageUtils* Description: 物理分页* Author* Date …...
【JAVA】包装类、正则表达式、Arrays类、Lambda表达式
1 包装类 包装类是8种基本数据类型对应的引用类型 作用:后期的集合和泛型不支持基本类型,只能使用包装类 基本数据类型和其对应的引用数据类型的变量可以互相赋值 基本数据类型引用数据类型 byte Byte short Short int Integer long Long ch…...
Java中的Maven Assembly插件是什么?
Maven Assembly插件是Maven中的一个插件,用于创建自定义的构建过程。它允许你在构建过程中执行一些自定义的操作,例如打包、编译、复制文件等。对于新手来说,Maven Assembly插件可能有点复杂,但是我们可以使用一些幽默的方式来解释…...
SpringBoot禁用Swagger3
Swagger3默认是启用的,即引入包就启用。 <dependency><groupId>io.springfox</groupId><artifactId>springfox-boot-starter</artifactId><version>3.0.0</version> </dependency> <dependency><groupId…...
小红书Java后端2023-8-6笔试
小红书推荐系统 时间限制:3000MS;内存限制:589824KB 题目描述 小红书有一个推荐系统,可以根据用户搜索的关键词推荐用户希望获取的内容。现在给定小孩的搜索记录(记录是分词后的结果),我们认…...
龙虎榜——20250610
上证指数放量收阴线,个股多数下跌,盘中受消息影响大幅波动。 深证指数放量收阴线形成顶分型,指数短线有调整的需求,大概需要一两天。 2025年6月10日龙虎榜行业方向分析 1. 金融科技 代表标的:御银股份、雄帝科技 驱动…...
web vue 项目 Docker化部署
Web 项目 Docker 化部署详细教程 目录 Web 项目 Docker 化部署概述Dockerfile 详解 构建阶段生产阶段 构建和运行 Docker 镜像 1. Web 项目 Docker 化部署概述 Docker 化部署的主要步骤分为以下几个阶段: 构建阶段(Build Stage):…...
【Python】 -- 趣味代码 - 小恐龙游戏
文章目录 文章目录 00 小恐龙游戏程序设计框架代码结构和功能游戏流程总结01 小恐龙游戏程序设计02 百度网盘地址00 小恐龙游戏程序设计框架 这段代码是一个基于 Pygame 的简易跑酷游戏的完整实现,玩家控制一个角色(龙)躲避障碍物(仙人掌和乌鸦)。以下是代码的详细介绍:…...
Zustand 状态管理库:极简而强大的解决方案
Zustand 是一个轻量级、快速和可扩展的状态管理库,特别适合 React 应用。它以简洁的 API 和高效的性能解决了 Redux 等状态管理方案中的繁琐问题。 核心优势对比 基本使用指南 1. 创建 Store // store.js import create from zustandconst useStore create((set)…...
Swift 协议扩展精进之路:解决 CoreData 托管实体子类的类型不匹配问题(下)
概述 在 Swift 开发语言中,各位秃头小码农们可以充分利用语法本身所带来的便利去劈荆斩棘。我们还可以恣意利用泛型、协议关联类型和协议扩展来进一步简化和优化我们复杂的代码需求。 不过,在涉及到多个子类派生于基类进行多态模拟的场景下,…...
对WWDC 2025 Keynote 内容的预测
借助我们以往对苹果公司发展路径的深入研究经验,以及大语言模型的分析能力,我们系统梳理了多年来苹果 WWDC 主题演讲的规律。在 WWDC 2025 即将揭幕之际,我们让 ChatGPT 对今年的 Keynote 内容进行了一个初步预测,聊作存档。等到明…...
什么是Ansible Jinja2
理解 Ansible Jinja2 模板 Ansible 是一款功能强大的开源自动化工具,可让您无缝地管理和配置系统。Ansible 的一大亮点是它使用 Jinja2 模板,允许您根据变量数据动态生成文件、配置设置和脚本。本文将向您介绍 Ansible 中的 Jinja2 模板,并通…...
宇树科技,改名了!
提到国内具身智能和机器人领域的代表企业,那宇树科技(Unitree)必须名列其榜。 最近,宇树科技的一项新变动消息在业界引发了不少关注和讨论,即: 宇树向其合作伙伴发布了一封公司名称变更函称,因…...
MySQL 部分重点知识篇
一、数据库对象 1. 主键 定义 :主键是用于唯一标识表中每一行记录的字段或字段组合。它具有唯一性和非空性特点。 作用 :确保数据的完整性,便于数据的查询和管理。 示例 :在学生信息表中,学号可以作为主键ÿ…...
Visual Studio Code 扩展
Visual Studio Code 扩展 change-case 大小写转换EmmyLua for VSCode 调试插件Bookmarks 书签 change-case 大小写转换 https://marketplace.visualstudio.com/items?itemNamewmaurer.change-case 选中单词后,命令 changeCase.commands 可预览转换效果 EmmyLua…...
