当前位置: 首页 > news >正文

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的题目都重新刷了一遍&#xff0c;明天开始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服务(详细)

目录&#xff1a;导读 前言 一、Python编程入门到精通二、接口自动化项目实战三、Web自动化项目实战四、App自动化项目实战五、一线大厂简历六、测试开发DevOps体系七、常用自动化测试工具八、JMeter性能测试九、总结&#xff08;尾部小惊喜&#xff09; 前言 Linux之docker搭…...

去了字节跳动,才知道年薪40W的测试有这么多?

今年大环境不好&#xff0c;内卷的厉害&#xff0c;薪资待遇好的工作机会更是难得。最近脉脉职言区有一条讨论火了&#xff1a; 哪家互联网公司薪资最‘厉害’&#xff1f; 下面的评论多为字节跳动&#xff0c;还炸出了很多年薪40W的测试工程师 我只想问一句&#xff0c;现在的…...

linux0.95(VFS重点)源码通俗解读(施工中)

文件系统在磁盘中的体现 下面是磁盘的内容&#xff0c;其中i节点就是一个inode数组&#xff0c;逻辑块就是数据块可用于存放数据 操作系统通过将磁盘数据读入到内存中指定的缓冲区块来与磁盘交互&#xff0c;对内存中的缓冲区块修改后写回磁盘。 进程(task_struct * task[N…...

mac ssh连接另一台window虚拟机vm

vmware配置端口映射 编辑(E) > 虚拟网络编辑器(N)... > NAT设置(S)... window防火墙&#xff0c;入站规则添加5555端口 控制面板 > 系统和安全 > Windows 防火墙>高级设置>入站规则>新建规则... tips windows查看端口命令&#xff1a;netstat -ano | f…...

使用Python解析通达信本地lday数据结构

通达信软件中的vipdoc是一个存储股票行情数据的文件夹。在通达信软件的安装目录下&#xff0c;可以找到一个名为vipdoc的文件夹&#xff0c;里面存放着各个股票的分时、日线、周线、月线等行情数据文件。这些数据文件可以用于自定义分析和回测股票的走势和交易策略&#xff0c;…...

【Mysql】修改definer

修改definer 本文介绍如何修改MySQL中的function、procedure、event、view和trigger的definer 修改function、procedure的definer 首先&#xff0c;我们需要登录MySQL命令行界面&#xff0c;然后执行以下命令&#xff1a; select definer from mysql.proc;这个命令会列出所…...

图片预览插件vue-photo-preview的使用

移动端项目中需要图片预览的功能&#xff0c;但本身使用mintui&#xff0c;vantui中虽然也有&#xff0c;但是为了一个组件安装这个有点儿多余&#xff0c;就选用了vue-photo-preview插件实现&#xff08;其实偷懒也不想自己写&#xff09;。 1、安装 npm i vue-photo-preview…...

最强自动化测试框架Playwright(20)- iframe

一个页面可以附加一个或多个 Frame 对象。每个页面都有一个主框架&#xff0c;并且假定页面级交互&#xff08;如&#xff09;在主框架中运行。click frame_locator 使用 iframe 时&#xff0c;可以创建一个框架定位器&#xff0c;该定位器将进入 iframe 并允许选择该 iframe…...

leetcode 516. 最长回文子序列(JAVA)题解

题目链接https://leetcode.cn/problems/longest-palindromic-subsequence/description/?utm_sourceLCUS&utm_mediumip_redirect&utm_campaigntransfer2china 目录 题目描述&#xff1a; 暴力递归&#xff1a; 动态规划&#xff1a; 题目描述&#xff1a; 给你一个…...

在Java中操作Redis(详细-->从环境配置到代码实现)

在Java中操作Redis 文章目录 在Java中操作Redis1、介绍2、Jedis3、Spring Data Redis3.1、对String的操作3.2、对哈希类型数据的操作3.3、对list的操作3.4、对set类型的操作3.5、对 ZSet类型的数据&#xff08;有序集合&#xff09;3.6、通用类型的操作 1、介绍 Redis 的Java客…...

分布式作业调度框架——ElasticJob

1、简介 ElasticJob 是面向互联网生态和海量任务的分布式调度解决方案&#xff0c;由两个相互独立的子项目 ElasticJob-Lite 和 ElasticJob-Cloud 组成。 它通过弹性调度、资源管控、以及作业治理的功能&#xff0c;打造一个适用于互联网场景的分布式调度解决方案&#xff0c;…...

react如何实现数据渲染

React数据渲染是指将组件中的数据映射到页面上&#xff0c;以展示出来。在React中&#xff0c;数据渲染通常是通过JSX和组件的state或props完成的。 JSX是一个类似HTML的语法&#xff0c;可以在其中嵌入JavaScript表达式。在JSX中&#xff0c;可以使用{}包裹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种基本数据类型对应的引用类型 作用&#xff1a;后期的集合和泛型不支持基本类型&#xff0c;只能使用包装类 基本数据类型和其对应的引用数据类型的变量可以互相赋值 基本数据类型引用数据类型 byte Byte short Short int Integer long Long ch…...

Java中的Maven Assembly插件是什么?

Maven Assembly插件是Maven中的一个插件&#xff0c;用于创建自定义的构建过程。它允许你在构建过程中执行一些自定义的操作&#xff0c;例如打包、编译、复制文件等。对于新手来说&#xff0c;Maven Assembly插件可能有点复杂&#xff0c;但是我们可以使用一些幽默的方式来解释…...

SpringBoot禁用Swagger3

Swagger3默认是启用的&#xff0c;即引入包就启用。 <dependency><groupId>io.springfox</groupId><artifactId>springfox-boot-starter</artifactId><version>3.0.0</version> </dependency> <dependency><groupId…...

小红书Java后端2023-8-6笔试

小红书推荐系统 时间限制&#xff1a;3000MS&#xff1b;内存限制&#xff1a;589824KB 题目描述 小红书有一个推荐系统&#xff0c;可以根据用户搜索的关键词推荐用户希望获取的内容。现在给定小孩的搜索记录&#xff08;记录是分词后的结果&#xff09;&#xff0c;我们认…...

利用ngx_stream_return_module构建简易 TCP/UDP 响应网关

一、模块概述 ngx_stream_return_module 提供了一个极简的指令&#xff1a; return <value>;在收到客户端连接后&#xff0c;立即将 <value> 写回并关闭连接。<value> 支持内嵌文本和内置变量&#xff08;如 $time_iso8601、$remote_addr 等&#xff09;&a…...

Python实现prophet 理论及参数优化

文章目录 Prophet理论及模型参数介绍Python代码完整实现prophet 添加外部数据进行模型优化 之前初步学习prophet的时候&#xff0c;写过一篇简单实现&#xff0c;后期随着对该模型的深入研究&#xff0c;本次记录涉及到prophet 的公式以及参数调优&#xff0c;从公式可以更直观…...

select、poll、epoll 与 Reactor 模式

在高并发网络编程领域&#xff0c;高效处理大量连接和 I/O 事件是系统性能的关键。select、poll、epoll 作为 I/O 多路复用技术的代表&#xff0c;以及基于它们实现的 Reactor 模式&#xff0c;为开发者提供了强大的工具。本文将深入探讨这些技术的底层原理、优缺点。​ 一、I…...

基于matlab策略迭代和值迭代法的动态规划

经典的基于策略迭代和值迭代法的动态规划matlab代码&#xff0c;实现机器人的最优运输 Dynamic-Programming-master/Environment.pdf , 104724 Dynamic-Programming-master/README.md , 506 Dynamic-Programming-master/generalizedPolicyIteration.m , 1970 Dynamic-Programm…...

Mysql中select查询语句的执行过程

目录 1、介绍 1.1、组件介绍 1.2、Sql执行顺序 2、执行流程 2.1. 连接与认证 2.2. 查询缓存 2.3. 语法解析&#xff08;Parser&#xff09; 2.4、执行sql 1. 预处理&#xff08;Preprocessor&#xff09; 2. 查询优化器&#xff08;Optimizer&#xff09; 3. 执行器…...

Go 语言并发编程基础:无缓冲与有缓冲通道

在上一章节中&#xff0c;我们了解了 Channel 的基本用法。本章将重点分析 Go 中通道的两种类型 —— 无缓冲通道与有缓冲通道&#xff0c;它们在并发编程中各具特点和应用场景。 一、通道的基本分类 类型定义形式特点无缓冲通道make(chan T)发送和接收都必须准备好&#xff0…...

云原生安全实战:API网关Kong的鉴权与限流详解

&#x1f525;「炎码工坊」技术弹药已装填&#xff01; 点击关注 → 解锁工业级干货【工具实测|项目避坑|源码燃烧指南】 一、基础概念 1. API网关&#xff08;API Gateway&#xff09; API网关是微服务架构中的核心组件&#xff0c;负责统一管理所有API的流量入口。它像一座…...

Razor编程中@Html的方法使用大全

文章目录 1. 基础HTML辅助方法1.1 Html.ActionLink()1.2 Html.RouteLink()1.3 Html.Display() / Html.DisplayFor()1.4 Html.Editor() / Html.EditorFor()1.5 Html.Label() / Html.LabelFor()1.6 Html.TextBox() / Html.TextBoxFor() 2. 表单相关辅助方法2.1 Html.BeginForm() …...

[大语言模型]在个人电脑上部署ollama 并进行管理,最后配置AI程序开发助手.

ollama官网: 下载 https://ollama.com/ 安装 查看可以使用的模型 https://ollama.com/search 例如 https://ollama.com/library/deepseek-r1/tags # deepseek-r1:7bollama pull deepseek-r1:7b改token数量为409622 16384 ollama命令说明 ollama serve #&#xff1a…...

(一)单例模式

一、前言 单例模式属于六大创建型模式,即在软件设计过程中,主要关注创建对象的结果,并不关心创建对象的过程及细节。创建型设计模式将类对象的实例化过程进行抽象化接口设计,从而隐藏了类对象的实例是如何被创建的,封装了软件系统使用的具体对象类型。 六大创建型模式包括…...