当前位置: 首页 > 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;我们认…...

STM32F4基本定时器使用和原理详解

STM32F4基本定时器使用和原理详解 前言如何确定定时器挂载在哪条时钟线上配置及使用方法参数配置PrescalerCounter ModeCounter Periodauto-reload preloadTrigger Event Selection 中断配置生成的代码及使用方法初始化代码基本定时器触发DCA或者ADC的代码讲解中断代码定时启动…...

镜像里切换为普通用户

如果你登录远程虚拟机默认就是 root 用户&#xff0c;但你不希望用 root 权限运行 ns-3&#xff08;这是对的&#xff0c;ns3 工具会拒绝 root&#xff09;&#xff0c;你可以按以下方法创建一个 非 root 用户账号 并切换到它运行 ns-3。 一次性解决方案&#xff1a;创建非 roo…...

Keil 中设置 STM32 Flash 和 RAM 地址详解

文章目录 Keil 中设置 STM32 Flash 和 RAM 地址详解一、Flash 和 RAM 配置界面(Target 选项卡)1. IROM1(用于配置 Flash)2. IRAM1(用于配置 RAM)二、链接器设置界面(Linker 选项卡)1. 勾选“Use Memory Layout from Target Dialog”2. 查看链接器参数(如果没有勾选上面…...

PAN/FPN

import torch import torch.nn as nn import torch.nn.functional as F import mathclass LowResQueryHighResKVAttention(nn.Module):"""方案 1: 低分辨率特征 (Query) 查询高分辨率特征 (Key, Value).输出分辨率与低分辨率输入相同。"""def __…...

推荐 github 项目:GeminiImageApp(图片生成方向,可以做一定的素材)

推荐 github 项目:GeminiImageApp(图片生成方向&#xff0c;可以做一定的素材) 这个项目能干嘛? 使用 gemini 2.0 的 api 和 google 其他的 api 来做衍生处理 简化和优化了文生图和图生图的行为(我的最主要) 并且有一些目标检测和切割(我用不到) 视频和 imagefx 因为没 a…...

Proxmox Mail Gateway安装指南:从零开始配置高效邮件过滤系统

&#x1f49d;&#x1f49d;&#x1f49d;欢迎莅临我的博客&#xff0c;很高兴能够在这里和您见面&#xff01;希望您在这里可以感受到一份轻松愉快的氛围&#xff0c;不仅可以获得有趣的内容和知识&#xff0c;也可以畅所欲言、分享您的想法和见解。 推荐&#xff1a;「storms…...

ubuntu22.04有线网络无法连接,图标也没了

今天突然无法有线网络无法连接任何设备&#xff0c;并且图标都没了 错误案例 往上一顿搜索&#xff0c;试了很多博客都不行&#xff0c;比如 Ubuntu22.04右上角网络图标消失 最后解决的办法 下载网卡驱动&#xff0c;重新安装 操作步骤 查看自己网卡的型号 lspci | gre…...

从零开始了解数据采集(二十八)——制造业数字孪生

近年来&#xff0c;我国的工业领域正经历一场前所未有的数字化变革&#xff0c;从“双碳目标”到工业互联网平台的推广&#xff0c;国家政策和市场需求共同推动了制造业的升级。在这场变革中&#xff0c;数字孪生技术成为备受关注的关键工具&#xff0c;它不仅让企业“看见”设…...

边缘计算网关提升水产养殖尾水处理的远程运维效率

一、项目背景 随着水产养殖行业的快速发展&#xff0c;养殖尾水的处理成为了一个亟待解决的环保问题。传统的尾水处理方式不仅效率低下&#xff0c;而且难以实现精准监控和管理。为了提升尾水处理的效果和效率&#xff0c;同时降低人力成本&#xff0c;某大型水产养殖企业决定…...

游戏开发中常见的战斗数值英文缩写对照表

游戏开发中常见的战斗数值英文缩写对照表 基础属性&#xff08;Basic Attributes&#xff09; 缩写英文全称中文释义常见使用场景HPHit Points / Health Points生命值角色生存状态MPMana Points / Magic Points魔法值技能释放资源SPStamina Points体力值动作消耗资源APAction…...