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

秋招算法刷题8

20240422

2.两数相加

时间复杂度O(max(m,n)),空间复杂度O(1)

public ListNode addTwoNumbers(ListNode l1, ListNode l2) {ListNode head=null,tail=null;int carry=0;while(l1!=null||l2!=null){int n1=l1!=null?l1.val:0;int n2=l2!=null?l2.val:0;int sum=n1+n2+carry;if(head==null){head=tail=new ListNode(sum%10);}else{tail.next=new ListNode(sum%10);tail=tail.next;}carry=sum/10;if(l1!=null){l1=l1.next;}if(l2!=null){l2=l2.next;}}if(carry>0){tail.next=new ListNode(carry);}return head;}

19.删除链表的倒数第N个节点

1.计算链表长度
时间复杂度O(n)空间复杂度O(1)

class Solution {public ListNode removeNthFromEnd(ListNode head, int n) {ListNode dummy=new ListNode(0,head);int length=getLength(head);ListNode cur=dummy;for(int i=1;i<length-n+1;++i){cur=cur.next;}cur.next=cur.next.next;ListNode ans=dummy.next;return ans;}public int getLength(ListNode head){int length=0;while(head!=null){++length;head=head.next;}return length;}
}

2.双指针法
时间复杂度O(n)空间复杂度O(1)

public ListNode removeNthFromEnd(ListNode head, int n) {ListNode dummy=new ListNode(0,head);ListNode first=head;ListNode second=dummy;for(int i=0;i<n;++i){first=first.next;}while(first!=null){first=first.next;second=second.next;}second.next=second.next.next;ListNode ans=dummy.next;return ans;}

24.俩两交换链表中的节点

1.递归
时间空间复杂度O(n)

class Solution {public ListNode swapPairs(ListNode head) {if(head==null||head.next==null){return head;}ListNode newHead=head.next;head.next=swapPairs(newHead.next);newHead.next=head;return newHead;}
}

2.迭代
时间复杂度O(n),空间复杂度O(1)

public ListNode swapPairs(ListNode head) {ListNode dummyHead=new ListNode(0);dummyHead.next=head;ListNode temp=dummyHead;while(temp.next!=null&&temp.next.next!=null){ListNode node1=temp.next;ListNode node2=temp.next.next;temp.next=node2;node1.next=node2.next;node2.next=node1;temp=node1;}return dummyHead.next;}

20240423

138.随机链表的复制

1.回溯+哈希表
时间复杂度和空间复杂度O(n)

class Solution {Map<Node,Node> cacheNode=new HashMap<Node,Node>();public Node copyRandomList(Node head) {if(head==null){return null;}if(!cacheNode.containsKey(head)){Node headNew=new Node(head.val);cacheNode.put(head,headNew);headNew.next=copyRandomList(head.next);headNew.random=copyRandomList(head.random);}return cacheNode.get(head);}
}

2.迭代+节点拆分
时间O(n),空间O(1)

public Node copyRandomList(Node head) {if(head==null){return null;}for(Node node=head;node!=null;node=node.next.next){Node nodeNew=new Node(node.val);nodeNew.next=node.next;node.next=nodeNew;}for(Node node=head;node!=null;node=node.next.next){Node nodeNew=node.next;nodeNew.random=(node.random!=null)?node.random.next:null;}Node headNew=head.next;for(Node node=head;node!=null;node=node.next){Node nodeNew=node.next;node.next=node.next.next;nodeNew.next=(nodeNew.next!=null)?nodeNew.next.next:null;}return headNew;}

14.最长公共前缀

1.横向扫描

public String longestCommonPrefix(String[] strs) {if(strs==null||strs.length==0){return "";}String prefix=strs[0];int count=strs.length;for(int i=1;i<count;i++){prefix=longestCommonPrefix(prefix,strs[i]);if(prefix.length()==0){break;}}return prefix;}public String longestCommonPrefix(String str1,String str2){int length=Math.min(str1.length(),str2.length());int index=0;while(index<length&&str1.charAt(index)==str2.charAt(index)){index++;}return str1.substring(0,index);}
}

2.纵向扫描

public String longestCommonPrefix(String[] strs) {if(strs==null||strs.length==0){return "";}int length=strs[0].length();int count=strs.length;for(int i=0;i<length;i++){char c=strs[0].charAt(i);for(int j=1;j<count;j++){if(i==strs[j].length()||strs[j].charAt(i)!=c){return strs[0].substring(0,i);}}}return strs[0];}

给定一个 n,算出 1 到 n 的最小公倍数

public static BigInteger f(int n){int[] x = new int[n+1];for(int i=1; i<=n; i++) x[i] = i;for(int i=2; i<n; i++){for(int j=i+1; j<=n; j++){if(x[j] % x[i]==0)x[j]=x[j]/x[i];}}//解决大数相乘BigInteger m = BigInteger.ONE;for(int i=2; i<=n; i++){m = m.multiply(BigInteger.valueOf((long)x[i]));}return m;}

二叉树的前、中、后遍历

1.非递归

https://www.bilibili.com/video/BV1GY4y1u7b2/?spm_id_from=pageDriver&vd_source=4bff775c9c6da7af76663b10f157a21f

2.递归

https://blog.csdn.net/loss_rose777/article/details/131399871

5.最长回文子串

1.动态规划
时间复杂度、空间O(n^2)

public String longestPalindrome(String s) {int len=s.length();if(len<2){return s;}int maxLen=1;int begin=0;boolean[][] dp=new boolean[len][len];for(int i=0;i<len;i++){dp[i][i]=true;}char[] charArray=s.toCharArray();for(int L=2;L<=len;L++){for(int i=0;i<len;i++){int j=L+i-1;if(j>=len){break;}if(charArray[i]!=charArray[j]){dp[i][j]=false;}else{if(j-i<3){dp[i][j]=true;}else{dp[i][j]=dp[i+1][j-1];}}if(dp[i][j]&&j-i+1>maxLen){maxLen=j-i+1;begin=i;}}}return s.substring(begin,begin+maxLen);}

2.中心扩展算法

class Solution {public String longestPalindrome(String s) {if(s==null||s.length()<1){return "";}int start=0,end=0;for(int i=0;i<s.length();i++){int len1=expandAroundCenter(s,i,i);int len2=expandAroundCenter(s,i,i+1);int len=Math.max(len1,len2);if(len>end-start){start=i-(len-1)/2;end=i+len/2;}}return s.substring(start,end+1);}public int expandAroundCenter(String s,int left,int right){while(left>=0&&right<s.length()&&s.charAt(left)==s.charAt(right)){--left;++right;}return right-left-1;}
}

20.回文子串

class Solution {public int countSubstrings(String s) {if(s==null||s.length()==0){return 0;}int count=0;for(int i=0;i<s.length();++i){count+=countPalindrome(s,i,i);count+=countPalindrome(s,i,i+1);}return count;}private int countPalindrome(String s,int start,int end){int count=0;while(start>=0&&end<s.length()&&s.charAt(start)==s.charAt(end)){count++;start--;end++;}return count;}
}

605.种花问题

public boolean canPlaceFlowers(int[] flowerbed, int n) {int count=0;int m=flowerbed.length;int prev=-1;for(int i=0;i<m;i++){if(flowerbed[i]==1){if(prev<0){count+=i/2;}else{count+=(i-prev-2)/2;}if(count>=n){return true;}prev=i;}}if(prev<0){count+=(m+1)/2;}else{count+=(m-prev-1)/2;}return count>=n;}

20240426

136.只出现一次的数字

华为od面试题:我23年5月7日竟然写过这个题,但是我一年后面试时候还是不会。都不知道我是太笨了呢,还是没努力呢
(其余出现2次)

public int singleNumber(int[] nums) {int single=0;for(int num:nums){single^=num;}return single;}

137.只出现一次的数字(其余出现三次)

1.哈希表
时间空间都是O(n)

public int singleNumber(int[] nums) {Map<Integer,Integer> freq=new HashMap<Integer,Integer>();for(int num:nums){freq.put(num,freq.getOrDefault(num,0)+1);}int ans=0;for(Map.Entry<Integer,Integer> entry:freq.entrySet()){int num=entry.getKey(),occ=entry.getValue();if(occ==1){ans=num;break;}}return ans;}

2.遍历统计

public int singleNumber(int[] nums) {int[] counts=new int[32];for(int num:nums){for(int j=0;j<32;j++){counts[j]+=num&1;num>>>=1;}}int res=0,m=3;for(int i=0;i<32;i++){res<<=1;res|=counts[31-i]%m;}return res;}

260.只出现一次的数字III

1.哈希表

public static int singleNumber(int[] nums) {Map<Integer,Integer> freq=new HashMap<Integer,Integer>();for(int num:nums){//freq.put(num, freq.getOrDefault(num,0)+1);freq.put(num,freq.containsKey(num)?freq.get(num)+1:1);}int ans=0;for(Map.Entry<Integer,Integer> entry:freq.entrySet()){int num=entry.getKey(),occ=entry.getValue();if(occ==1){ans=num;break;}}return ans;}

2.位运算

public int[] singleNumber(int[] nums) {int xorsum=0;for(int num:nums){xorsum^=num;}int lowbit=xorsum&-xorsum;int[] ans=new int[2];for(int x:nums){ans[(x&lowbit)==0?0:1]^=x;}return ans;}

相关文章:

秋招算法刷题8

20240422 2.两数相加 时间复杂度O&#xff08;max(m,n))&#xff0c;空间复杂度O&#xff08;1&#xff09; public ListNode addTwoNumbers(ListNode l1, ListNode l2) {ListNode headnull,tailnull;int carry0;while(l1!null||l2!null){int n1l1!null?l1.val:0;int n2l2!…...

Docker使用方法

Docker是一种容器化平台&#xff0c;它可以帮助开发人员将应用程序和其依赖项打包成一个独立的、可移植的容器&#xff0c;以便在不同的环境中运行。 以下是使用Docker的基本步骤&#xff1a; 安装Docker&#xff1a;首先&#xff0c;您需要在您的机器上安装Docker。您可以从D…...

HTML学习|网页基本信息、网页基本标签、图像标签、超链接标签、列表标签、表格标签、媒体元素、页面结构分析、iframe内联框架

网页基本信息 DOCTYPE是设置使用什么规范&#xff0c;网页整个信息都在html标签中&#xff0c;head标签里包含字符集设置&#xff0c;网页介绍等信息&#xff0c;title标签是网页的名称&#xff0c;网页的主干都在body标签中 网页基本标签 标题标签 h1~h6都是标题标签&#x…...

001 websocket(评论功能demo)(消息推送)

文章目录 ReviewController.javaWebSocketConfig.javaWebSocketProcess.javaServletInitializer.javaWebsocketApplication.javareadmeindex.htmlapplication.yamlpom.xml ReviewController.java package com.example.controller;import com.example.websocket.WebSocketProces…...

二分查找向下取整导致的死循环69. x 的平方根

二分查找向下取整导致的死循环 考虑伪题目&#xff1a;从数组arr中查找出目标元素target对应的下标&#xff0c;如果数组中不存在目标元素&#xff0c;找 到第一个元素值小于target的元素的下标。 编写二分查找算法如下&#xff1a; Testvoid testBinarySearch(){int[] arr n…...

Kivy 异步任务

如果要进行一些非常耗时的操作(例如&#xff1a;爬虫等)&#xff0c;那么页面就会在这里卡住&#xff0c;而系统就会以为这个软件无响应&#xff0c;并提示关闭&#xff0c;可以说明用户体验极差&#xff0c;因此我们在此处引入异步操作。 在py中引入事件调节器&#xff0c;并在…...

DEV--C++小游戏(吃星星(0.1))

目录 吃星星&#xff08;0.1&#xff09; 简介 头文件 命名空间变量 副函数 清屏函数 打印地图函数 移动函数 主函数 0.1版完整代码 吃星星&#xff08;0.1&#xff09; 注&#xff1a;版本<1为未实现或只实现部分 简介 用wasd去吃‘*’ 头文件 #include<bi…...

LINUX 入门 4

LINUX 入门 4 day6 7 20240429 20240504 耗时&#xff1a;240min 课程链接地址 第4章 LINUX环境编程——实现线程池 C基础 第3节 #define里面的行不能乱空行&#xff0c;要换行就打\ typedef 是 C 和 C 中的一个关键字&#xff0c;用于为已有的数据类型定义一个新的名字。…...

Imagine Flash、StyleMamba 、FlexControl、Multi-Scene T2V、TexControl

本文首发于公众号&#xff1a;机器感知 Imagine Flash、StyleMamba 、FlexControl、Multi-Scene T2V、TexControl You Only Cache Once: Decoder-Decoder Architectures for Language Models We introduce a decoder-decoder architecture, YOCO, for large language models, …...

Java Collections.emptyList() 方法详解

前言 在Java开发的日常中&#xff0c;我们常常需要处理集合数据结构&#xff0c;而这其中就免不了要面对“空集合”的场景。传统的做法可能是直接返回 null&#xff0c;但这往往会引入空指针异常的风险&#xff0c;降低了代码的健壮性。幸运的是&#xff0c;Java为我们提供了一…...

Vue前端环境准备

vue-cli Vue-cli是Vue官方提供的脚手架&#xff0c;用于快速生成一个Vue项目模板 提供功能&#xff1a; 统一的目录结构 本地调试 热部署 单元测试 集成打包上线 依赖环境&#xff1a;NodeJs 安装NodeJs与Vue-Cli 1、安装nodejs&#xff08;已经安装就不用了&#xff09; node-…...

代码随想录算法训练营第四十二天| 01背包问题(二维、一维)、416.分割等和子集

系列文章目录 目录 系列文章目录动态规划&#xff1a;01背包理论基础①二维数组②一维数组&#xff08;滚动数组&#xff09; 416. 分割等和子集①回溯法&#xff08;超时&#xff09;②动态规划&#xff08;01背包&#xff09;未剪枝版剪枝版 动态规划&#xff1a;01背包理论基…...

故障——蓝桥杯十三届2022国赛大学B组真题

问题分析 这道题纯数学&#xff0c;考察贝叶斯公式 AC_Code #include <bits/stdc.h> using namespace std; typedef pair<int,double> PI; bool cmp(PI a,PI b){if(a.second!b.second)return a.second>b.second;return a.first<b.first; } int main() {i…...

SSD存储基本知识

存储技术随着时间的推移经历了显著变化&#xff0c;新兴的存储介质正逐步挑战已经成为行业标准的硬盘驱动器&#xff08;HDD&#xff09;。在众多竞争者中&#xff0c;固态硬盘&#xff08;SSD&#xff09;是最广泛采用且最有潜力占据主导地位的——它们速度快、运行安静&#…...

buuctf-misc题目练习二

ningen 打开题目后是一张图片&#xff0c;放进winhex里面 发现PK&#xff0c;PK是压缩包ZIP 文件的文件头&#xff0c;下一步是想办法进行分离 Foremost可以依据文件内的文件头和文件尾对一个文件进行分离&#xff0c;或者识别当前的文件是什么文件。比如拓展名被删除、被附加…...

Nginx rewrite项目练习

Nginx rewrite练习 1、访问ip/xcz&#xff0c;返回400状态码&#xff0c;要求用rewrite匹配/xcz a、访问/xcz返回400 b、访问/hello时正常访问xcz.html页面server {listen 192.168.99.137:80;server_name 192.168.99.137;charset utf-8;root /var/www/html;location / {root …...

2024,AI手机“元年”? | 最新快讯

文 | 伯虎财经&#xff0c;作者 | 铁观音 2024年&#xff0c;小米、荣耀、vivo、一加、努比亚等品牌的AI手机新品如雨后春笋般涌现。因此&#xff0c;这一年也被业界广泛视为AI手机的“元年” 试想&#xff0c;当你轻触屏幕&#xff0c;你的手机不仅响应你的指令&#xff0c;更…...

5月9(信息差)

&#x1f30d; 可再生能源发电量首次占全球电力供应的三成 &#x1f384;马斯克脑机接口公司 Neuralink 计划将 Link 功能扩展至现实世界&#xff0c;实现控制机械臂、轮椅等 马斯克脑机接口公司 Neuralink 计划将 Link 功能扩展至现实世界&#xff0c;实现控制机械臂、轮椅等…...

leetcode203-Remove Linked List Elements

题目 给你一个链表的头节点 head 和一个整数 val &#xff0c;请你删除链表中所有满足 Node.val val 的节点&#xff0c;并返回 新的头节点 。 示例 1&#xff1a; 输入&#xff1a;head [1,2,6,3,4,5,6], val 6 输出&#xff1a;[1,2,3,4,5] 示例 2&#xff1a; 输入&…...

2024付费进群系统,源码及搭建变现视频课程(教程+源码)

自从我做资源站项目盈利稳定后&#xff0c;我越来越对网站类项目感兴趣了&#xff0c;毕竟很多网站类项目还是需要一定技术门槛的&#xff0c;可以过滤掉一些人&#xff0c;很多新人做项目就只盯着短视频&#xff0c;所以网站类项目也就没那么的卷。 这个付费进群系统&#xf…...

零门槛NAS搭建:WinNAS如何让普通电脑秒变私有云?

一、核心优势&#xff1a;专为Windows用户设计的极简NAS WinNAS由深圳耘想存储科技开发&#xff0c;是一款收费低廉但功能全面的Windows NAS工具&#xff0c;主打“无学习成本部署” 。与其他NAS软件相比&#xff0c;其优势在于&#xff1a; 无需硬件改造&#xff1a;将任意W…...

iOS 26 携众系统重磅更新,但“苹果智能”仍与国行无缘

美国西海岸的夏天&#xff0c;再次被苹果点燃。一年一度的全球开发者大会 WWDC25 如期而至&#xff0c;这不仅是开发者的盛宴&#xff0c;更是全球数亿苹果用户翘首以盼的科技春晚。今年&#xff0c;苹果依旧为我们带来了全家桶式的系统更新&#xff0c;包括 iOS 26、iPadOS 26…...

Spark 之 入门讲解详细版(1)

1、简介 1.1 Spark简介 Spark是加州大学伯克利分校AMP实验室&#xff08;Algorithms, Machines, and People Lab&#xff09;开发通用内存并行计算框架。Spark在2013年6月进入Apache成为孵化项目&#xff0c;8个月后成为Apache顶级项目&#xff0c;速度之快足见过人之处&…...

以下是对华为 HarmonyOS NETX 5属性动画(ArkTS)文档的结构化整理,通过层级标题、表格和代码块提升可读性:

一、属性动画概述NETX 作用&#xff1a;实现组件通用属性的渐变过渡效果&#xff0c;提升用户体验。支持属性&#xff1a;width、height、backgroundColor、opacity、scale、rotate、translate等。注意事项&#xff1a; 布局类属性&#xff08;如宽高&#xff09;变化时&#…...

【HarmonyOS 5.0】DevEco Testing:鸿蒙应用质量保障的终极武器

——全方位测试解决方案与代码实战 一、工具定位与核心能力 DevEco Testing是HarmonyOS官方推出的​​一体化测试平台​​&#xff0c;覆盖应用全生命周期测试需求&#xff0c;主要提供五大核心能力&#xff1a; ​​测试类型​​​​检测目标​​​​关键指标​​功能体验基…...

DAY 47

三、通道注意力 3.1 通道注意力的定义 # 新增&#xff1a;通道注意力模块&#xff08;SE模块&#xff09; class ChannelAttention(nn.Module):"""通道注意力模块(Squeeze-and-Excitation)"""def __init__(self, in_channels, reduction_rat…...

Java - Mysql数据类型对应

Mysql数据类型java数据类型备注整型INT/INTEGERint / java.lang.Integer–BIGINTlong/java.lang.Long–––浮点型FLOATfloat/java.lang.FloatDOUBLEdouble/java.lang.Double–DECIMAL/NUMERICjava.math.BigDecimal字符串型CHARjava.lang.String固定长度字符串VARCHARjava.lang…...

Java多线程实现之Callable接口深度解析

Java多线程实现之Callable接口深度解析 一、Callable接口概述1.1 接口定义1.2 与Runnable接口的对比1.3 Future接口与FutureTask类 二、Callable接口的基本使用方法2.1 传统方式实现Callable接口2.2 使用Lambda表达式简化Callable实现2.3 使用FutureTask类执行Callable任务 三、…...

【算法训练营Day07】字符串part1

文章目录 反转字符串反转字符串II替换数字 反转字符串 题目链接&#xff1a;344. 反转字符串 双指针法&#xff0c;两个指针的元素直接调转即可 class Solution {public void reverseString(char[] s) {int head 0;int end s.length - 1;while(head < end) {char temp …...

GitHub 趋势日报 (2025年06月06日)

&#x1f4ca; 由 TrendForge 系统生成 | &#x1f310; https://trendforge.devlive.org/ &#x1f310; 本日报中的项目描述已自动翻译为中文 &#x1f4c8; 今日获星趋势图 今日获星趋势图 590 cognee 551 onlook 399 project-based-learning 348 build-your-own-x 320 ne…...