秋招算法刷题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(max(m,n)),空间复杂度O(1) 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是一种容器化平台,它可以帮助开发人员将应用程序和其依赖项打包成一个独立的、可移植的容器,以便在不同的环境中运行。 以下是使用Docker的基本步骤: 安装Docker:首先,您需要在您的机器上安装Docker。您可以从D…...
HTML学习|网页基本信息、网页基本标签、图像标签、超链接标签、列表标签、表格标签、媒体元素、页面结构分析、iframe内联框架
网页基本信息 DOCTYPE是设置使用什么规范,网页整个信息都在html标签中,head标签里包含字符集设置,网页介绍等信息,title标签是网页的名称,网页的主干都在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 的平方根
二分查找向下取整导致的死循环 考虑伪题目:从数组arr中查找出目标元素target对应的下标,如果数组中不存在目标元素,找 到第一个元素值小于target的元素的下标。 编写二分查找算法如下: Testvoid testBinarySearch(){int[] arr n…...
Kivy 异步任务
如果要进行一些非常耗时的操作(例如:爬虫等),那么页面就会在这里卡住,而系统就会以为这个软件无响应,并提示关闭,可以说明用户体验极差,因此我们在此处引入异步操作。 在py中引入事件调节器,并在…...
DEV--C++小游戏(吃星星(0.1))
目录 吃星星(0.1) 简介 头文件 命名空间变量 副函数 清屏函数 打印地图函数 移动函数 主函数 0.1版完整代码 吃星星(0.1) 注:版本<1为未实现或只实现部分 简介 用wasd去吃‘*’ 头文件 #include<bi…...
LINUX 入门 4
LINUX 入门 4 day6 7 20240429 20240504 耗时:240min 课程链接地址 第4章 LINUX环境编程——实现线程池 C基础 第3节 #define里面的行不能乱空行,要换行就打\ typedef 是 C 和 C 中的一个关键字,用于为已有的数据类型定义一个新的名字。…...
Imagine Flash、StyleMamba 、FlexControl、Multi-Scene T2V、TexControl
本文首发于公众号:机器感知 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开发的日常中,我们常常需要处理集合数据结构,而这其中就免不了要面对“空集合”的场景。传统的做法可能是直接返回 null,但这往往会引入空指针异常的风险,降低了代码的健壮性。幸运的是,Java为我们提供了一…...
Vue前端环境准备
vue-cli Vue-cli是Vue官方提供的脚手架,用于快速生成一个Vue项目模板 提供功能: 统一的目录结构 本地调试 热部署 单元测试 集成打包上线 依赖环境:NodeJs 安装NodeJs与Vue-Cli 1、安装nodejs(已经安装就不用了) node-…...
代码随想录算法训练营第四十二天| 01背包问题(二维、一维)、416.分割等和子集
系列文章目录 目录 系列文章目录动态规划:01背包理论基础①二维数组②一维数组(滚动数组) 416. 分割等和子集①回溯法(超时)②动态规划(01背包)未剪枝版剪枝版 动态规划:01背包理论基…...
故障——蓝桥杯十三届2022国赛大学B组真题
问题分析 这道题纯数学,考察贝叶斯公式 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存储基本知识
存储技术随着时间的推移经历了显著变化,新兴的存储介质正逐步挑战已经成为行业标准的硬盘驱动器(HDD)。在众多竞争者中,固态硬盘(SSD)是最广泛采用且最有潜力占据主导地位的——它们速度快、运行安静&#…...
buuctf-misc题目练习二
ningen 打开题目后是一张图片,放进winhex里面 发现PK,PK是压缩包ZIP 文件的文件头,下一步是想办法进行分离 Foremost可以依据文件内的文件头和文件尾对一个文件进行分离,或者识别当前的文件是什么文件。比如拓展名被删除、被附加…...
Nginx rewrite项目练习
Nginx rewrite练习 1、访问ip/xcz,返回400状态码,要求用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手机“元年”? | 最新快讯
文 | 伯虎财经,作者 | 铁观音 2024年,小米、荣耀、vivo、一加、努比亚等品牌的AI手机新品如雨后春笋般涌现。因此,这一年也被业界广泛视为AI手机的“元年” 试想,当你轻触屏幕,你的手机不仅响应你的指令,更…...
5月9(信息差)
🌍 可再生能源发电量首次占全球电力供应的三成 🎄马斯克脑机接口公司 Neuralink 计划将 Link 功能扩展至现实世界,实现控制机械臂、轮椅等 马斯克脑机接口公司 Neuralink 计划将 Link 功能扩展至现实世界,实现控制机械臂、轮椅等…...
leetcode203-Remove Linked List Elements
题目 给你一个链表的头节点 head 和一个整数 val ,请你删除链表中所有满足 Node.val val 的节点,并返回 新的头节点 。 示例 1: 输入:head [1,2,6,3,4,5,6], val 6 输出:[1,2,3,4,5] 示例 2: 输入&…...
2024付费进群系统,源码及搭建变现视频课程(教程+源码)
自从我做资源站项目盈利稳定后,我越来越对网站类项目感兴趣了,毕竟很多网站类项目还是需要一定技术门槛的,可以过滤掉一些人,很多新人做项目就只盯着短视频,所以网站类项目也就没那么的卷。 这个付费进群系统…...
SkyWalking 10.2.0 SWCK 配置过程
SkyWalking 10.2.0 & SWCK 配置过程 skywalking oap-server & ui 使用Docker安装在K8S集群以外,K8S集群中的微服务使用initContainer按命名空间将skywalking-java-agent注入到业务容器中。 SWCK有整套的解决方案,全安装在K8S群集中。 具体可参…...
应用升级/灾备测试时使用guarantee 闪回点迅速回退
1.场景 应用要升级,当升级失败时,数据库回退到升级前. 要测试系统,测试完成后,数据库要回退到测试前。 相对于RMAN恢复需要很长时间, 数据库闪回只需要几分钟。 2.技术实现 数据库设置 2个db_recovery参数 创建guarantee闪回点,不需要开启数据库闪回。…...
线程与协程
1. 线程与协程 1.1. “函数调用级别”的切换、上下文切换 1. 函数调用级别的切换 “函数调用级别的切换”是指:像函数调用/返回一样轻量地完成任务切换。 举例说明: 当你在程序中写一个函数调用: funcA() 然后 funcA 执行完后返回&…...
STM32F4基本定时器使用和原理详解
STM32F4基本定时器使用和原理详解 前言如何确定定时器挂载在哪条时钟线上配置及使用方法参数配置PrescalerCounter ModeCounter Periodauto-reload preloadTrigger Event Selection 中断配置生成的代码及使用方法初始化代码基本定时器触发DCA或者ADC的代码讲解中断代码定时启动…...
MySQL用户和授权
开放MySQL白名单 可以通过iptables-save命令确认对应客户端ip是否可以访问MySQL服务: test: # iptables-save | grep 3306 -A mp_srv_whitelist -s 172.16.14.102/32 -p tcp -m tcp --dport 3306 -j ACCEPT -A mp_srv_whitelist -s 172.16.4.16/32 -p tcp -m tcp -…...
基于 TAPD 进行项目管理
起因 自己写了个小工具,仓库用的Github。之前在用markdown进行需求管理,现在随着功能的增加,感觉有点难以管理了,所以用TAPD这个工具进行需求、Bug管理。 操作流程 注册 TAPD,需要提供一个企业名新建一个项目&#…...
快刀集(1): 一刀斩断视频片头广告
一刀流:用一个简单脚本,秒杀视频片头广告,还你清爽观影体验。 1. 引子 作为一个爱生活、爱学习、爱收藏高清资源的老码农,平时写代码之余看看电影、补补片,是再正常不过的事。 电影嘛,要沉浸,…...
【p2p、分布式,区块链笔记 MESH】Bluetooth蓝牙通信 BLE Mesh协议的拓扑结构 定向转发机制
目录 节点的功能承载层(GATT/Adv)局限性: 拓扑关系定向转发机制定向转发意义 CG 节点的功能 节点的功能由节点支持的特性和功能决定。所有节点都能够发送和接收网格消息。节点还可以选择支持一个或多个附加功能,如 Configuration …...
wpf在image控件上快速显示内存图像
wpf在image控件上快速显示内存图像https://www.cnblogs.com/haodafeng/p/10431387.html 如果你在寻找能够快速在image控件刷新大图像(比如分辨率3000*3000的图像)的办法,尤其是想把内存中的裸数据(只有图像的数据,不包…...
系统掌握PyTorch:图解张量、Autograd、DataLoader、nn.Module与实战模型
本文较长,建议点赞收藏,以免遗失。更多AI大模型应用开发学习视频及资料,尽在聚客AI学院。 本文通过代码驱动的方式,系统讲解PyTorch核心概念和实战技巧,涵盖张量操作、自动微分、数据加载、模型构建和训练全流程&#…...
