面試算法刷題練習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.pswp.cn/pingmian/79828.shtml
繁體地址,請注明出處:http://hk.pswp.cn/pingmian/79828.shtml
英文地址,請注明出處:http://en.pswp.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;整座房子已經被完全掏…