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

初次使用Taotoken平台从注册到完成API调用的全程指引

&#x1f680; 告别海外账号与网络限制&#xff01;稳定直连全球优质大模型&#xff0c;限时半价接入中。 &#x1f449; 点击领取海量免费额度 初次使用Taotoken平台从注册到完成API调用的全程指引 对于初次接触大模型API的开发者而言&#xff0c;从注册平台到成功发出第一个…...

扩散模型如何重塑建筑设计流程:从概念生成到性能优化的AI协作

1. 项目概述&#xff1a;当AI成为建筑师的“副驾驶”几年前&#xff0c;当我在设计院通宵达旦地对着屏幕调整一个曲面屋顶的参数时&#xff0c;我就在想&#xff0c;有没有一种工具&#xff0c;能让我把脑子里那个模糊的意象&#xff0c;瞬间变成可供推敲的视觉草稿&#xff1f…...

从“意大利面”到整洁代码:我是如何用SonarQube重构遗留项目的

从“意大利面”到整洁代码&#xff1a;我是如何用SonarQube重构遗留项目的 接手一个结构混乱的遗留项目&#xff0c;就像面对一盘煮过头的意大利面——各种逻辑纠缠不清&#xff0c;随便动一处就可能引发连锁反应。去年我遇到这样一个Java项目&#xff1a;12万行代码&#xff0…...

Pytorch图像去噪实战(七十四):Kubernetes部署图像去噪服务,实现容器编排和弹性扩展

Pytorch图像去噪实战(七十四):Kubernetes部署图像去噪服务,实现容器编排和弹性扩展 一、问题场景:Docker Compose够用,但多服务扩展开始吃力 前面我们用 Docker Compose 部署了图像去噪服务。 Compose 对单机部署非常好用,但当项目变复杂后,会遇到: 多台机器部署困难…...

避坑指南:用Qt为STM32项目写上位机时,我遇到的5个串口和界面难题

避坑指南&#xff1a;用Qt为STM32项目写上位机时&#xff0c;我遇到的5个串口和界面难题 第一次用Qt给STM32开发上位机时&#xff0c;我以为串口通信不过是简单的数据收发&#xff0c;界面设计拖拖控件就能搞定。直到项目进度被各种诡异bug拖慢两周后&#xff0c;才意识到自己踩…...

Claude API代理网关:开源项目newaiproxy/claude-api架构解析与部署实战

1. 项目概述&#xff1a;一个连接Claude的API代理网关如果你正在尝试将Claude的对话能力集成到自己的应用里&#xff0c;或者想绕过官方Web界面的一些限制&#xff0c;那么你很可能已经听说过或者正在寻找一个可靠的API代理方案。newaiproxy/claude-api这个项目&#xff0c;本质…...

汽车芯片市场深度解析:从电动化、智能化到供应链变革

1. 汽车芯片行业&#xff1a;短期阵痛与长期增长的辩证观最近和几个在车厂和Tier 1供应商做研发的老朋友聊天&#xff0c;大家普遍的感觉是&#xff1a;冰火两重天。一边是终端市场感觉“卷”得厉害&#xff0c;销量波动、价格战不停&#xff1b;另一边&#xff0c;研发部门的芯…...

轻量级代码同步工具codesyncer:P2P架构实现跨设备实时同步

1. 项目概述&#xff1a;一个被低估的代码同步利器如果你和我一样&#xff0c;经常需要在多台开发机、服务器甚至不同的云环境之间同步代码片段、配置文件或者小型项目&#xff0c;那你一定对那种“这台机器上有&#xff0c;那台机器上没有”的混乱感同身受。手动复制粘贴&…...

“面”之跃升:系统化协同的演进与企业级智能体

展望2026 年&#xff0c;AI 能力的演进或将正式迈入“面”的维度。这是一种“系统化协同”&#xff0c;意味着AI 与企业核心IT 系统、组织架构以及外部生态实现了深度融合。 系统化协同的特征&#xff0c;从 “面”的层级看&#xff0c;AI 不再是一个外挂的工具或独立的流程&am…...

CTF出题人视角:我是如何设计ctfshow F5杯那些“脑洞大开”的MISC题的

CTF出题人视角&#xff1a;如何设计令人拍案叫绝的MISC赛题 在CTF竞赛中&#xff0c;MISC&#xff08;杂项&#xff09;题目往往是最能体现创意与思维碰撞的领域。作为F5杯的核心出题人之一&#xff0c;我想分享几个设计"脑洞题"的底层逻辑——这些题目后来被参赛选手…...