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

使用VSCode开发Django指南

使用VSCode开发Django指南 一、概述 Django 是一个高级 Python 框架&#xff0c;专为快速、安全和可扩展的 Web 开发而设计。Django 包含对 URL 路由、页面模板和数据处理的丰富支持。 本文将创建一个简单的 Django 应用&#xff0c;其中包含三个使用通用基本模板的页面。在此…...

SkyWalking 10.2.0 SWCK 配置过程

SkyWalking 10.2.0 & SWCK 配置过程 skywalking oap-server & ui 使用Docker安装在K8S集群以外&#xff0c;K8S集群中的微服务使用initContainer按命名空间将skywalking-java-agent注入到业务容器中。 SWCK有整套的解决方案&#xff0c;全安装在K8S群集中。 具体可参…...

鸿蒙中用HarmonyOS SDK应用服务 HarmonyOS5开发一个医院挂号小程序

一、开发准备 ​​环境搭建​​&#xff1a; 安装DevEco Studio 3.0或更高版本配置HarmonyOS SDK申请开发者账号 ​​项目创建​​&#xff1a; File > New > Create Project > Application (选择"Empty Ability") 二、核心功能实现 1. 医院科室展示 /…...

【2025年】解决Burpsuite抓不到https包的问题

环境&#xff1a;windows11 burpsuite:2025.5 在抓取https网站时&#xff0c;burpsuite抓取不到https数据包&#xff0c;只显示&#xff1a; 解决该问题只需如下三个步骤&#xff1a; 1、浏览器中访问 http://burp 2、下载 CA certificate 证书 3、在设置--隐私与安全--…...

Robots.txt 文件

什么是robots.txt&#xff1f; robots.txt 是一个位于网站根目录下的文本文件&#xff08;如&#xff1a;https://example.com/robots.txt&#xff09;&#xff0c;它用于指导网络爬虫&#xff08;如搜索引擎的蜘蛛程序&#xff09;如何抓取该网站的内容。这个文件遵循 Robots…...

鸿蒙中用HarmonyOS SDK应用服务 HarmonyOS5开发一个生活电费的缴纳和查询小程序

一、项目初始化与配置 1. 创建项目 ohpm init harmony/utility-payment-app 2. 配置权限 // module.json5 {"requestPermissions": [{"name": "ohos.permission.INTERNET"},{"name": "ohos.permission.GET_NETWORK_INFO"…...

Linux 内存管理实战精讲:核心原理与面试常考点全解析

Linux 内存管理实战精讲&#xff1a;核心原理与面试常考点全解析 Linux 内核内存管理是系统设计中最复杂但也最核心的模块之一。它不仅支撑着虚拟内存机制、物理内存分配、进程隔离与资源复用&#xff0c;还直接决定系统运行的性能与稳定性。无论你是嵌入式开发者、内核调试工…...

腾讯云V3签名

想要接入腾讯云的Api&#xff0c;必然先按其文档计算出所要求的签名。 之前也调用过腾讯云的接口&#xff0c;但总是卡在签名这一步&#xff0c;最后放弃选择SDK&#xff0c;这次终于自己代码实现。 可能腾讯云翻新了接口文档&#xff0c;现在阅读起来&#xff0c;清晰了很多&…...

《Offer来了:Java面试核心知识点精讲》大纲

文章目录 一、《Offer来了:Java面试核心知识点精讲》的典型大纲框架Java基础并发编程JVM原理数据库与缓存分布式架构系统设计二、《Offer来了:Java面试核心知识点精讲(原理篇)》技术文章大纲核心主题:Java基础原理与面试高频考点Java虚拟机(JVM)原理Java并发编程原理Jav…...

webpack面试题

面试题&#xff1a;webpack介绍和简单使用 一、webpack&#xff08;模块化打包工具&#xff09;1. webpack是把项目当作一个整体&#xff0c;通过给定的一个主文件&#xff0c;webpack将从这个主文件开始找到你项目当中的所有依赖文件&#xff0c;使用loaders来处理它们&#x…...