面试算法刷题练习1(核心+acm)

3. 无重复字符的最长子串

核心代码模式
class Solution {public int lengthOfLongestSubstring(String s) {int len=s.length();int []num=new int[300];int ans=0;for(int i=0,j=0;i<len;i++){num[s.charAt(i)]++;while(num[s.charAt(i)]>1){num[s.charAt(j)]--;j++;}ans=Math.max(ans,i-j+1);}return ans;}
}
手写输入输出模式 
import java.util.*;public class Javaacm
{public static void main(String []args){
//输入:abadsadasScanner scan=new Scanner(System.in);String s=scan.next();int len=s.length();int ans=0;int num[]=new int[300];for(int i=0,j=0;i<len;i++){num[s.charAt(i)]++;while(num[s.charAt(i)]>1){num[s.charAt(j)]--;j++;}ans=Math.max(ans,i-j+1);}System.out.println(ans);}
}

146. LRU 缓存

核心代码模式
class LRUCache {int capacity;   Map<Integer,Node> m=new HashMap<>();Node dummy=new Node(0,0);class Node{int key,value;Node pre ,ne;Node(int k,int v){key=k;value=v;}}public LRUCache(int capacity) {this.capacity=capacity;dummy.pre=dummy;dummy.ne=dummy;}public int get(int key) {Node cur=getNode(key);return cur==null?-1:cur.value;}Node getNode(int key){if(!m.containsKey(key))return null;Node cur=m.get(key);remove(cur);pushFront(cur);return cur;}void remove(Node node){node.pre.ne=node.ne;node.ne.pre=node.pre;}void pushFront(Node node){node.ne=dummy.ne;node.pre=dummy;node.pre.ne=node;node.ne.pre=node;}//逻辑最复杂public void put(int key, int value) {Node cur=getNode(key);if(cur!=null){cur.value=value;return ;}    cur=new Node(key,value);m.put(key,cur);pushFront(cur);if(capacity<m.size()){m.remove(dummy.pre.key);remove(dummy.pre);}}
}
手写输入输出模式 
import java.util.*;
class lrucache
{//双向链表+HashMapprivate final int capacity;//容量private final Node dummy=new Node(0,0);//双向链表头结点Map<Integer,Node> m=new HashMap<>();//哈希表public static class Node{ //node类,构造节点int key ,value;Node pre,ne;Node(int k,int v){key=k;value=v;}}public lrucache(int capacity)  //初始化,设定容量{this.capacity=capacity;dummy.pre=dummy;dummy.ne=dummy;}//get,获取节点的value,没有则为-1public int get(int key){Node node=getNode(key);   //获取节点并更新到最近使用return node!=null?node.value:-1;}//获取节点病更新到最近使用private Node getNode(int key){if(!m.containsKey(key))return null;  //没有则返回nullNode node=m.get(key);     //获取该节点linkRemove(node);        //双向链表中删除该结点linkPushFront(node);      //插入到头部return node;}void linkRemove(Node x){//删除某节点x.ne.pre=x.pre;x.pre.ne=x.ne;}
void linkPushFront(Node x)
{//从头部插入节点x.pre=dummy;x.ne=dummy.ne;x.ne.pre=x;x.pre.ne=x;
}public void put(int key,int value)
{  //先获取节点,顺便更新到最近使用Node node=getNode(key);if(node!=null){node.value=value;   return ;       //存在则更新值并返回}node=new Node(key,value);m.put(key,node);      //哈希表插入linkPushFront(node);    //双向链表插入if(m.size()>capacity){    //容量超过上限m.remove(dummy.pre.key);    //哈希表删除最久使用linkRemove(dummy.pre);          //双向链表移除该节点}
}
}
public class Javaacm
{public static void main(String []args){lrucache lru=new lrucache(2);lru.put(1,1);lru.put(2,2);System.out.println(lru.get(2));  //2lru.put(3,3); System.out.println(lru.get(1));  //-1System.out.println(lru.get(3));  //3}
}

206. 反转链表

核心代码模式
/*** Definition for singly-linked list.* public class ListNode {*     int val;*     ListNode next;*     ListNode() {}*     ListNode(int val) { this.val = val; }*     ListNode(int val, ListNode next) { this.val = val; this.next = next; }* }*/
class Solution {public ListNode reverseList(ListNode head) {ListNode pre=null,cur=head;//双指针while(cur!=null){ListNode ne=cur.next;//留存剩余节点的头部cur.next=pre;   //双指针更新pre=cur;cur=ne;}return pre;//最后cur为null,pre为新的头节点}
}
手写输入输出模式 
import java.util.*;
class ListNode
{int val;ListNode next;ListNode() {}ListNode(int val) { this.val = val; }ListNode(int val, ListNode next) { this.val = val; this.next = next;}
}
public class Javaacm
{public static void main(String []args){
//输入3,2,1,4,5Scanner scanner=new Scanner(System.in);String inner[]=scanner.next().split(",");   //输入ListNode  head=new ListNode(Integer.valueOf(inner[0])); //创建第一个节点,这里默认不会传空ListNode pre=null,cur=head;for(int i=1;i<inner.length;i++){    //循环构造链表ListNode curNode =new ListNode(Integer.valueOf(inner[i]));cur.next=curNode;cur=curNode;}//开始反转链表cur=head;while(cur!=null){ListNode ne=cur.next;cur.next=pre;pre=cur;cur=ne;}//输出即可while(pre!=null){System.out.print(pre.val+"->");pre=pre.next;}System.out.print("null");//5->4->3->2->1->null}
}

215. 数组中的第K个最大元素

PriorityQueue写法

核心代码模式
class Solution {public int findKthLargest(int[] nums, int k) {Queue<Integer> q=new PriorityQueue<>();for(int i=0;i<nums.length;i++){q.add(nums[i]);if(q.size()>k){q.poll();}}return q.poll();}
}
手写输入输出
import java.util.*;public class Javaacm
{public static void main(String []args){
//输入:
//[3,2,1,5,6,4]
//3Scanner scanner=new Scanner(System.in);String s=scanner.next();int k=scanner.nextInt();String str[]=s.substring(1,s.length()-1).split(",");Queue<Integer> q=new PriorityQueue<>();for(int i=0;i<str.length;i++){int cur=Integer.valueOf(str[i]);q.add(cur);if(q.size()>k){q.poll();}}System.out.print(q.poll());}
}

快速选择算法

核心代码模式
class Solution {int nums[];public int findKthLargest(int[] num, int k) {nums=num;int n=nums.length;return f(0,n-1,n-k);}void swap(int i,int j){int t=nums[i];nums[i]=nums[j];nums[j]=t;}int f(int l,int r,int k){if(l>=r)return nums[k];int x=nums[l],i=l-1,j=r+1;while(i<j){do i++;while(nums[i]<x);do j--;while(nums[j]>x);if(i<j)swap(i,j);}if(k<=j)return f(l,j,k);return f(j+1,r,k);}
}
手写输入输出
import java.util.*;public class Javaacm
{static int nums[];public static void main(String []args){
//输入:
//[3,2,1,5,6,4]
//3Scanner scanner=new Scanner(System.in);String s=scanner.next();int k=scanner.nextInt();String str[]=s.substring(1,s.length()-1).split(",");nums=new int[str.length];for(int i=0;i<str.length;i++)nums[i]=Integer.valueOf(str[i]);System.out.print(quickselect(0,nums.length-1,nums.length-k));}static int quickselect(int l,int r,int k){if(l>=r)return nums[k];int x=nums[l],i=l-1,j=r+1;while(i<j){do i++;while(nums[i]<x);do j--;while(nums[j]>x);if(i<j)swap(i,j);}if(k<=j)return quickselect(l,j,k);return quickselect(j+1,r,k);}static void swap(int a,int b){int t=nums[a];nums[a]=nums[b];nums[b]=t;}
}

25. K 个一组翻转链表

核心代码模式
/*** Definition for singly-linked list.* public class ListNode {*     int val;*     ListNode next;*     ListNode() {}*     ListNode(int val) { this.val = val; }*     ListNode(int val, ListNode next) { this.val = val; this.next = next; }* }*/
class Solution {public ListNode reverseKGroup(ListNode head, int k) {ListNode pre=null,cur=head,hh=new ListNode(0,head);ListNode before=hh;int len=0;while(cur!=null){len++;cur=cur.next;}cur=head;for(int i=len;i>=k;i-=k){for(int j=0;j<k;j++){ListNode ne=cur.next;cur.next=pre;pre=cur;cur=ne;}ListNode ne=before.next;before.next=pre;before=ne;ne.next=cur;}return hh.next;}
}
手写输入输出
import java.util.*;class ListNode
{int val;ListNode next;ListNode(){}ListNode(String v){val=Integer.valueOf(v);}ListNode(int v,ListNode ne){val=v;next=ne;}
}
public class Javaacm
{
//输入格式
// [3,2,1,5,6,4]
// 3public static void main(String []args){Scanner scanner=new Scanner(System.in);String s=scanner.next();int k=scanner.nextInt();String str[]=s.substring(1,s.length()-1).split(",");int len=str.length;ListNode head=new ListNode(str[0]);ListNode pre=head;for(int i=1;i<len;i++){ListNode cur=new ListNode(str[i]);pre.next=cur;pre=cur;}ListNode cur=head;ListNode hh=new ListNode(0,head);ListNode before=hh;for(int l=len;l>=k;l-=k){for(int i=0;i<k;i++){ListNode ne=cur.next;cur.next=pre;pre=cur;cur=ne;}ListNode ne=before.next;  before.next=pre;  //改头部before=ne;        //换指针ne.next=cur;      //衔尾部}ListNode p=hh.next;for(int i=0;i<len;i++){System.out.print(p.val+"->");p=p.next;}System.out.print("null");}
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/pingmian/79828.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

拉削丝锥,螺纹类加工的选择之一

在我们的日常生活中&#xff0c;螺纹连接无处不在&#xff0c;从简单的螺丝钉到复杂的机械设备&#xff0c;都离不开螺纹的精密加工。今天&#xff0c;给大家介绍一种的螺纹刀具——拉削丝锥&#xff1a; 一、拉削丝锥的工作原理 拉削丝锥&#xff0c;听起来有点陌生吧&#…

数据清洗-电商双11美妆数据分析(二)

1.接下来用seaborn包给出每个店铺各个大类以及各个小类的销量销售额 先观察销量&#xff0c;各店小类中销量最高的是相宜本草的补水类商品以及妮维雅的清洁类商品&#xff0c;这两类销量很接近。而销售额上&#xff0c;相宜本草的补水类商品比妮维雅的清洁类商品要高得多&#…

【上位机——MFC】对话框

对话框的使用 1.添加对话框资源 2.定义一个自己的对话框类(CMyDlg)&#xff0c;管理对话框资源&#xff0c;派生自CDialog或CDialogEx均可 对话框架构 #include <afxwin.h> #include "resource.h"class CMyDlg :public CDialog {DECLARE_MESSAGE_MAP() publi…

2025东三省C题深圳杯C题数学建模挑战赛数模思路代码文章教学: 分布式能源接入配电网的风险分析

完整内容请看文章最下面的推广群 数据整理与分析 表1&#xff1a;有源配电网62节点系统负荷参数 内容&#xff1a;列出了62个节点的有功负荷&#xff08;单位&#xff1a;kW&#xff09;。 特点&#xff1a; 负荷范围&#xff1a;24 kW&#xff08;节点19&#xff09;到420 …

【人工智能】边缘计算技术及应用概述

边缘计算&#xff08;Edge Computing&#xff09;是一种分布式计算范式&#xff0c;其核心思想是将数据处理、存储和计算能力从传统的云端数据中心下沉到靠近数据源的边缘设备&#xff08;如传感器、摄像头、工业设备等&#xff09;或边缘服务器。这种架构旨在减少数据传输延迟…

FAISS(Facebook AI Similarity Search)

First steps with Faiss for k-nearest neighbor search in large search spaces - Davide’s GitHub pages FAISS&#xff08;Facebook AI Similarity Search&#xff09;是由Meta&#xff08;原Facebook&#xff09;AI团队开发的高效相似性搜索库&#xff0c;主要用于处理大规…

嵌入式开发学习日志Day15

一、指针指向字符型数组 &#xff08;1&#xff09;【const】&#xff1a;在指针变量中使用时&#xff0c;无法通过该指针修改被指向的变量&#xff1b; &#xff08;2&#xff09;【const】&#xff1a;关键字&#xff0c;在C和C中&#xff0c;能加就加&#xff0c;加了一定…

现代卷积神经网络

一、网络中的网络 (NiN: Network in Network) 参考&#xff1a;Network In Network——卷积神经网络的革新 - 殷大侠 - 博客园 深度学习&#xff08;二十六&#xff09;Network In Network学习笔记-CSDN博客 ① MLPconv 层 参考&#xff1a;深度学习基础模型NIN(Network in Net…

【大模型面试每日一题】Day 11:参数高效微调方法(如LoRA、Adapter)的核心思想是什么?相比全参数微调有何优缺点?

【大模型面试每日一题】Day 11&#xff1a;参数高效微调方法&#xff08;如LoRA、Adapter&#xff09;的核心思想是什么&#xff1f;相比全参数微调有何优缺点&#xff1f; &#x1f4cc; 题目重现 &#x1f31f;&#x1f31f; 面试官&#xff1a;参数高效微调方法&#xff0…

SSL泄露源IP怎么办?(教学与防护)

在网络安全领域&#xff0c;源IP地址的保护至关重要。通常情况下&#xff0c;我们借助CDN&#xff08;内容分发网络&#xff09;技术来隐藏源IP&#xff0c;使外部通过常规的ping命令无法获取。然而&#xff0c;由于部分网站模板存在漏洞&#xff0c;当用户访问https://ip时&am…

jQuery的学习要领

学习 jQuery 的关键要领可以分为以下几个核心部分&#xff0c;帮助你高效掌握并灵活运用&#xff1a; 1. 理解 jQuery 的核心思想 "Write Less, Do More"&#xff1a;jQuery 通过简洁的语法封装复杂操作。 链式调用&#xff08;Chaining&#xff09;&#xff1a;通过…

网络安全的原理和基本知识点

以下是网络安全的基本原理和知识点&#xff0c;以及如何利用Python进行网络安全防护&#xff1a; 网络安全的基本原理和知识点 基本概念 网络安全&#xff1a;保护网络系统和数据免受攻击、损坏或未经授权的访问&#xff0c;确保其机密性、完整性和可用性。 CIA三要素 机密…

AI:机器学习之无监督学习

无监督学习:让机器从“混沌”中自我觉醒 🧠🌌 🧭 摘要:无监督学习(Unsupervised Learning)是机器学习的重要分支,它不依赖于人工标签,通过自身“感知”数据结构来发现潜在模式。本文系统梳理了其核心概念、典型算法、实际应用与代码实战,既适合入门学习,也适用于…

写了个脚本将pdf转markdown

看到有人需要将扫描pdf文档转markdown&#xff0c;想起之前写的一个小工具。 这个脚本是为了将pdf转成markdown&#xff0c;只需要申请一个智谱的api key&#xff0c;并填到config里&#xff0c;使用的模型是4v flash&#xff0c;免费的&#xff0c;所以可以放心使用。 效果如下…

CSS--图片链接水平居中展示的方法

原文网址&#xff1a;CSS--图片链接居中展示的方法-CSDN博客 简介 本文介绍CSS图片链接水平居中展示的方法。 图片链接 问题复现 源码 <html xml:lang"cn" lang"cn"><head><meta http-equiv"Content-Type" content"te…

工具分享:通过滑块拉取CAN报文信号数值自动发送报文

0. 概述 CAN报文发送工具使用wxpython进行开发,配套Excel模板可以通过修改Excel自定义界面展示的信号名称和信号的属性;同时,工具支持导入现场采集的报文数据自动按照配套Excel模板定义的报文发送周期进行模拟发送。 由于是我好几年前开发的作品,一些开发细节也记得不是很…

【Python】os模块

os 模块是 Python 标准库中用于与操作系统交互的核心模块&#xff0c;提供了许多操作文件和目 录的函数。 1. 基本介绍 os 模块提供了以下主要功能&#xff1a; 文件和目录操作路径操作进程管理环境变量访问 import os2. 常用功能分类 2.1 文件和目录操作 函数/方法描述o…

ai agent(智能体)开发 python3基础11: java 调用python waitfor卡死,导致深入理解操作系统进程模型和IPC机制

java 调用python waitfor 卡死 导致浏览器无法自动关闭&#xff0c;java &#xff0c;python双发无限等待 根源在于还是没有理解 进程之间标准输入输出到底是什么含义 系统进程与跨语言调用的核心机制 在跨语言调用&#xff08;如Java调用Python&#xff09;时&#xff0c;理…

Kubernetes(k8s)学习笔记(九)--搭建多租户系统

K8s 多租户管理 多租户是指在同一集群中隔离多个用户或团队&#xff0c;以避免他们之间的资源冲突和误操作。在K8s中&#xff0c;多租户管理的核心目标是在保证安全性的同时&#xff0c;提高资源利用率和运营效率。 在K8s中&#xff0c;该操作可以通过命名空间&#xff08;Nam…

同质化的旅游内核

湘西凤凰古城、北京非常有文艺氛围的方家胡同都在被改造翻新为现代的其他城市范式式的样式。 什么意思呢&#xff1f;很多古城的老房子&#xff0c;从外面看&#xff0c;很古老、很漂亮&#xff0c;但是进去以后&#xff0c;完全不是那么回事&#xff0c;整座房子已经被完全掏…