菜雞的原地踏步史(???)

leetcode啟動!(╯‵□′)╯︵┻━┻
嘗試改掉想到哪寫哪的代碼壞習慣

鏈表
相交鏈表

public class Solution {/**ac(公共長度)b所以 鏈表A的長度 a + c,鏈表B的長度b + ca + b + c = b + c + a只要指針a從headA開始走,走完再回到headB指針b從headB開始走,走完回到headA兩個指針相遇的地方一定是鏈表交點*/public ListNode getIntersectionNode(ListNode headA, ListNode headB) {ListNode pA = headA;ListNode pB = headB;while(pA != pB) {pA = (pA == null) ? headB : pA.next;pB = (pB == null) ? headA : pB.next;}return pA;}
}

反轉鏈表

class Solution {/**設置一個null結點pre作為反轉后的尾結點curr結點一路向后遍歷因為要改變結點指向,所以需要用temp保存curr下一個結點開寫~*/public ListNode reverseList(ListNode head) {ListNode pre = null;ListNode cur = head;while(cur != null) {ListNode temp = cur.next;cur.next = pre;pre = cur;cur = temp;}return pre;}
}

回文鏈表

class Solution {/**反轉鏈表,和舊鏈表逐個元素比較主打一個耗時耗力*/public boolean isPalindrome(ListNode head) {ListNode pre = null;ListNode cur = head;ListNode copy = head;ListNode old = new ListNode(head.val);ListNode oldList = old;while(copy != null) {// System.out.println(copy.val);copy = copy.next;if(copy != null) {ListNode temp = new ListNode(copy.val);old.next = temp;old = old.next;}}old.next = null;while(cur != null) {ListNode temp = cur.next;cur.next = pre;pre = cur;cur = temp;}ListNode newList = pre;while(newList != null) {if(newList.val != oldList.val) {return false;}newList = newList.next;oldList = oldList.next;}return true;}
}

環形鏈表

public class Solution {/**使用快慢指針slow一次走一步,fast一次走兩步快慢指針一定會在環內相遇,記為meet點這個時候slow從head開始走,meet從相遇點開始走,兩者一定會在環入口相遇*/public boolean hasCycle(ListNode head) {ListNode slow = head;ListNode fast = head;ListNode connect = null;while(fast != null && fast.next != null) {slow = slow.next;fast = fast.next.next;if(slow == fast) {connect = slow;return true;}}return connect == null ? false : true;}
}

環形鏈表II

public class Solution {/**原來上一題的判斷寫成了這一題的找環的入口思路一樣接著上一題找到meet點此時只要slow再次從頭開始走,meet指針向后走,兩者就一定會在環入口處相遇*/public ListNode detectCycle(ListNode head) {ListNode slow = head;ListNode fast = head;while(fast != null && fast.next != null) {slow = slow.next;fast = fast.next.next;if(slow == fast) {ListNode meet = slow;slow = head;while(slow != meet) {slow = slow.next;meet = meet.next;}return slow;}}return null;}
}

合并兩個有序鏈表

class Solution {/**樸實無華的想法新建一個頭結點null,進行結點挑選list1.val > list2.val ? 好,挑list2的結點來復制list1.val <= list2.val ? 好,挑list1的結點來復制如果list1或者list2走到頭了,就直接選擇不空的一連(因為已經排好序了)需要注意的是,所有的情況用if-else分好,否則會執行出錯*/public ListNode mergeTwoLists(ListNode list1, ListNode list2) {ListNode p = new ListNode(-1);ListNode res = p;while(list1 != null || list2 != null) {if(list1 == null) {p.next = list2;return res.next;}else if(list2 == null) {p.next = list1;return res.next;}else {if(list1.val > list2.val) {ListNode temp = new ListNode(list2.val);p.next = temp;p = p.next;list2 = list2.next;}else {ListNode temp = new ListNode(list1.val);p.next = temp;p = p.next;list1 = list1.next;}}}return res.next;}
}

兩數相加
(什么破代碼又臭又長 (┙>∧<)┙へ┻┻)

import java.math.BigInteger;
class Solution {/**樸實無華的想法,逆序想到棧道先進后出功能,使用棧保存數據時間空間效率你是一點不考慮啊苦路西測試用例有超出Long范圍的,需要BigIntegerBigInteger b1 = new BigInteger("2020031920200319");BigInteger b2 = new BigInteger("2020031820200318");//加法BigInteger add = b1.add(b2);System.out.println(add);//減法BigInteger subtract = b1.subtract(b2);System.out.println(subtract);//乘法BigInteger multiply = b1.multiply(b2);System.out.println(multiply);//除法BigInteger divide = b1.divide(b2);System.out.println(divide);*/public ListNode addTwoNumbers(ListNode l1, ListNode l2) {Stack<Integer> stack = new Stack();int count1 = 0;ListNode p1 = l1;while(p1 != null) {stack.push(p1.val);p1 = p1.next;count1++;}BigInteger sum1 = new BigInteger("0");BigInteger base = BigInteger.TEN;while(!stack.isEmpty()) {int temp = stack.pop();count1--;BigInteger tempAsBigInteger = BigInteger.valueOf(temp);BigInteger ten = base.pow(count1);BigInteger mul = tempAsBigInteger.multiply(ten);sum1 = sum1.add(mul);}int count2 = 0;ListNode p2 = l2;while(p2 != null) {stack.push(p2.val);count2++;p2 = p2.next;}BigInteger sum2 = new BigInteger("0");while(!stack.isEmpty()) {int temp = stack.pop();count2--;BigInteger tempAsBigInteger = BigInteger.valueOf(temp);BigInteger ten = base.pow(count2);BigInteger mul = tempAsBigInteger.multiply(ten);sum2 = sum2.add(mul);}System.out.println(sum1);System.out.println(sum2);BigInteger sum = new BigInteger("0");sum = sum1.add(sum2);System.out.println(sum);ListNode res = new ListNode(0);ListNode r = res;if((sum.compareTo(BigInteger.ZERO)) == 0) {return r;}while((sum.compareTo(BigInteger.ZERO)) != 0) {BigInteger x = sum.mod(base);// System.out.println(x);int x1 = x.intValue();// System.out.println(x1);ListNode temp = new ListNode((int)x1);res.next = temp;res = res.next;sum  = sum.divide(base);}return r.next;}
}

刪除鏈表點倒數第N個結點

class Solution {/**快指針fast先走N布然后慢指針slow從頭開始,和fast一起走,當fast走到頭了,slow在的位置就是要刪除的位置1 - 2 - 3 - 4 - null-2   1   0--       --        -刪        */public ListNode removeNthFromEnd(ListNode head, int n) {ListNode p = new ListNode(-1);p.next = head;ListNode res = p;ListNode fast = p;ListNode slow = p;while(n > 0) {n--;fast = fast.next;}while(fast.next != null) {slow = slow.next;fast = fast.next;}slow.next = slow.next.next;return p.next;}
}

兩兩交換鏈表結點

class Solution {/**-1 - 1 - 2 - 3 - 41 保存一下pre-1 - 2 - 3 - 43 - 4 保存一下after-1 - 2 - 1-1 - 2 - 1 - 3 - 4*/public ListNode swapPairs(ListNode head) {ListNode p = new ListNode(-1);ListNode res = p;p.next = head;while(p.next != null && p.next.next != null) {ListNode pre = p.next;p.next = p.next.next;ListNode after = p.next.next;p.next.next = pre;pre.next = after;p = pre;}return res.next;}
}

K個一組鏈表反轉

class Solution {/**只需要想遞歸最后一次null <- 2 <- 1  3 -> 4 -> 5 -> null2.next = reverseGroup(., k)*/public ListNode reverseKGroup(ListNode head, int k) {if(head == null) {return null;}ListNode a = head;ListNode b = head;for(int i = 0; i < k; i++) {if(b == null) {return head;}b = b.next; // 判斷要在指針后移之前啊啊啊啊}// 一開始用的while k--, 忘了把k從0恢復ListNode newHead = reverseKNodes(a, b);a.next = reverseKGroup(b, k);return newHead;}public ListNode reverseKNodes(ListNode a, ListNode b) {ListNode pre = null;ListNode cur = a;while(cur != b) {ListNode temp = cur.next;cur.next = pre;pre = cur;cur = temp;}return pre;}
}

隨機鏈表的復制

class Solution {/**感覺和拷貝鏈表沒有什么區別多了個random嘛, 用hashmap存*/public Node copyRandomList(Node head) {Node h = head;HashMap<Node, Node> map = new HashMap<>();Node newHead = new Node(-1);Node h1 = newHead;while(h != null) {Node temp = new Node(h.val);map.put(h, temp);newHead.next = temp;newHead = newHead.next;h = h.next;}h = head;Node h2 = h1.next;while(h != null) {h2.random = map.get(h.random);h = h.next;h2 = h2.next;}return h1.next;}
}

排序鏈表

class Solution {/**樸實無華的想法用List存鏈表元素,Collections.sort()進行排序,再新建一個鏈表*/public ListNode sortList(ListNode head) {List<Integer> list = new ArrayList();ListNode h = head;ListNode sortedList = new ListNode(-1);ListNode res = sortedList;while(h != null) {list.add(h.val);h = h.next;}Collections.sort(list);for(int i = 0; i < list.size(); i++) {ListNode temp = new ListNode(list.get(i));sortedList.next = temp;sortedList = sortedList.next;}return res.next;}
}

合并K個有序鏈表

class Solution {/**使用最小堆注意:lists是一個數組,是用lists.length獲取長度*/public ListNode mergeKLists(ListNode[] lists) {if(lists.length == 0) {return null;}ListNode p = new ListNode(-1);ListNode res = p;PriorityQueue<ListNode> pq = new PriorityQueue<>(lists.length, (a,b) -> (a.val - b.val));for(ListNode head :lists) {if(head != null) {pq.add(head);}}while(!pq.isEmpty()) {ListNode node = pq.poll();p.next = node;if(node.next != null) {pq.add(node.next);}p = p.next;}return res.next;}
}

LRU緩存

class LRUCache {/**八股問過,內存淘汰策略,刪除存活時間最久的keyallkesy-lru*/int cap;LinkedHashMap<Integer, Integer> cache = new LinkedHashMap<>();public LRUCache(int capacity) {this.cap = capacity;}public int get(int key) {if(!cache.containsKey(key)) {return -1;}makeRecently(key);return cache.get(key);}public void put(int key, int val) {if(cache.containsKey(key)) {cache.put(key, val);makeRecently(key);return;}if(cache.size() >= this.cap) {int oldestKey = cache.keySet().iterator().next();cache.remove(oldestKey);}cache.put(key, val);}private void makeRecently(int key) {int val = cache.get(key);cache.remove(key);cache.put(key, val);}
}

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/diannao/41118.shtml
繁體地址,請注明出處:http://hk.pswp.cn/diannao/41118.shtml
英文地址,請注明出處:http://en.pswp.cn/diannao/41118.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

利用pg_rman進行備份與恢復操作

文章目錄 pg_rman簡介一、安裝配置pg_rman二、創建表與用戶三、備份與恢復 pg_rman簡介 pg_rman 是 PostgreSQL 的在線備份和恢復工具。類似oracle 的 rman pg_rman 項目的目標是提供一種與 pg_dump 一樣簡單的在線備份和 PITR 方法。此外&#xff0c;它還為每個數據庫集群維護…

抖音使矛,美團用盾

有市場&#xff0c;就有競爭。抖音全力進軍本地生活市場欲取代美團&#xff0c;已不是新聞。 互聯網行業進入存量時代&#xff0c;本地生活市場是為數不多存在較大增長空間的賽道。艾媒咨詢數據顯示&#xff0c;預計2025年在線餐飲外賣市場規模達到17469億元&#xff0c;生鮮電…

Day05-01-jenkins進階

Day05-01-jenkins進階 10. 案例07: 理解 案例06基于ans實現10.1 整體流程10.2 把shell改為Ansible劇本10.3 jk調用ansible全流程10.4 書寫劇本 11. Jenkins進階11.1 jenkins分布式1&#xff09;概述2&#xff09;案例08&#xff1a;拆分docker功能3&#xff09;創建任務并綁定到…

安裝 ClamAV 并進行病毒掃描

安裝 ClamAV 并進行病毒掃描 以下是安裝 ClamAV 并使用它進行病毒掃描的步驟&#xff1a; 1. 安裝 ClamAV 在 Debian/Ubuntu 系統上&#xff1a; sudo apt update sudo apt install clamav clamav-daemon在 RHEL/CentOS 系統上&#xff1a; sudo yum install epel-release…

開發指南040-swagger加header

swagger可以在線生成接口文檔&#xff0c;便于前后端溝通&#xff0c;而且還可以在線調用接口&#xff0c;方便后臺調試。但是接口需要經過登錄校驗&#xff0c;部分接口還需要得到登錄token&#xff0c;使用token識別用戶身份進行后續操作。這種情況下&#xff0c;都需要接口增…

【刷題筆記(編程題)05】另類加法、走方格的方案數、井字棋、密碼強度等級

1. 另類加法 給定兩個int A和B。編寫一個函數返回AB的值&#xff0c;但不得使用或其他算數運算符。 測試樣例&#xff1a; 1,2 返回&#xff1a;3 示例 1 輸入 輸出 思路1: 二進制0101和1101的相加 0 1 0 1 1 1 0 1 其實就是 不帶進位的結果1000 和進位產生的1010相加 無進位加…

ssm校園志愿服務信息系統-計算機畢業設計源碼97697

摘 要 隨著社會的進步和信息技術的發展&#xff0c;越來越多的學校開始重視志愿服務工作&#xff0c;通過組織各種志愿服務活動&#xff0c;讓學生更好地了解社會、服務社會。然而&#xff0c;在實際操作中&#xff0c;志愿服務的組織和管理面臨著諸多問題&#xff0c;如志愿者…

dledger原理源碼分析系列(一)-架構,核心組件和rpc組件

簡介 dledger是openmessaging的一個組件&#xff0c; raft算法實現&#xff0c;用于分布式日志&#xff0c;本系列分析dledger如何實現raft概念&#xff0c;以及dledger在rocketmq的應用 本系列使用dledger v0.40 本文分析dledger的架構&#xff0c;核心組件&#xff1b;rpc組…

【pytorch16】MLP反向傳播

鏈式法則回顧 多輸出感知機的推導公式回顧 只與w相關的輸出節點和輸入節點有關 多層多輸入感知機 擴展為多層感知機的話&#xff0c;意味著還有一些層&#xff08;理解為隱藏層σ函數&#xff09;&#xff0c;暫且設置為 x j x_{j} xj?層 對于 x j x_{j} xj?層如果把前面的…

迅捷PDF編輯器合并PDF

迅捷PDF編輯器是一款專業的PDF編輯軟件&#xff0c;不僅支持任意添加文本&#xff0c;而且可以任意編輯PDF原有內容&#xff0c;軟件上方的工具欄中還有豐富的PDF標注、編輯功能&#xff0c;包括高亮、刪除線、下劃線這些基礎的&#xff0c;還有規則或不規則框選、箭頭、便利貼…

【護眼小知識】護眼臺燈真的護眼嗎?防近視臺燈有效果嗎?

當前&#xff0c;近視問題在人群中愈發普遍&#xff0c;據2024年的統計數據顯示&#xff0c;我國兒童青少年的總體近視率已高達52.7%。并且近視背后潛藏著諸多眼部并發癥的風險&#xff0c;例如視網膜脫離、白內障以及開角型青光眼等&#xff0c;嚴重的情況甚至可能引發失明。為…

PMP--知識卡片--波士頓矩陣

文章目錄 記憶黑話概念作用圖示 記憶 一說到波士頓就聯想到波士頓龍蝦&#xff0c;所以波士頓矩陣跟動物有關&#xff0c;狗&#xff0c;牛。 黑話 你公司的現金牛業務&#xff0c;正在逐漸變成瘦狗&#xff0c;應盡快采取收割策略&#xff1b;問題業務的儲備太少&#xff0…

必須掌握的Linux的九大命令

ifconfig 命令用于配置和查看網絡接口的參數。 ping 命令用于測試主機之間的網絡連通性。 telnet用于通過Telnet協議連接到遠程主機。 telnet 127.0.0.1 8000 telnet example.com telnet example.com 8080 iostat 命令用于報告 CPU 統計信息和 I/O 設備負載。 iostat&…

護眼熱點:臺燈護眼是真的嗎?一起來看臺燈的功能作用有哪些

如今近視問題日益嚴峻&#xff0c;尤為引人矚目的是&#xff0c;高度近視學生群體占比已逼近10%的警戒線&#xff0c;且這一比例伴隨著學齡的增長而悄然攀升——從幼兒園6歲孩童中那令人憂慮的1.5%&#xff0c;到高中階段驚人的17.6%&#xff0c;每一組數據都敲響了保護兒童視力…

【Linux】靜態庫的制作和使用詳解

&#x1f490; &#x1f338; &#x1f337; &#x1f340; &#x1f339; &#x1f33b; &#x1f33a; &#x1f341; &#x1f343; &#x1f342; &#x1f33f; &#x1f344;&#x1f35d; &#x1f35b; &#x1f364; &#x1f4c3;個人主頁 &#xff1a;阿然成長日記 …

代碼隨想錄算法訓練營第71天:路徑算法[1]

代碼隨想錄算法訓練營第71天&#xff1a;路徑算法 ? bellman_ford之單源有限最短路 卡碼網&#xff1a;96. 城市間貨物運輸 III(opens new window) 【題目描述】 某國為促進城市間經濟交流&#xff0c;決定對貨物運輸提供補貼。共有 n 個編號為 1 到 n 的城市&#xff0c…

【CT】LeetCode手撕—4. 尋找兩個正序數組的中位數

目錄 題目1- 思路2- 實現?4. 尋找兩個正序數組的中位數——題解思路 3- ACM 實現 題目 原題連接&#xff1a;4. 尋找兩個正序數組的中位數 1- 思路 思路 將尋找中位數 ——> 尋找兩個合并數組的第 K 大 &#xff08;K代表中位數&#xff09; 實現 ① 遍歷兩個數組 &am…

企業級監控系統Zabbix

文章目錄 Zabbix介紹Zabbix架構Zabbix serverZabbix agentZabbix proxy Zabbix Server的安裝Zabbix Agent的安裝監控主機流程zabbix_get自定義模板和監控項實戰用戶登錄數監控1.指定監控項命令2.重啟Agent服務3.在Server上創建監控項4.測試監控項5.查看監控項圖形 觸發器定義觸…

外泌體相關基因肝癌臨床模型預測——2-3分純生信文章復現——4.預后相關外泌體基因確定單因素cox回歸(2)

內容如下&#xff1a; 1.外泌體和肝癌TCGA數據下載 2.數據格式整理 3.差異表達基因篩選 4.預后相關外泌體基因確定 5.拷貝數變異及突變圖譜 6.外泌體基因功能注釋 7.LASSO回歸篩選外泌體預后模型 8.預后模型驗證 9.預后模型魯棒性分析 10.獨立預后因素分析及與臨床的…

【若依】關閉當前標簽頁并跳轉路由到其他頁面

使用場景如&#xff1a;當在新增/編輯路由頁面提交成功后&#xff0c;需要關閉當前頁&#xff0c;并跳轉回列表頁。 實現代碼&#xff1a; this.$store.dispatch("tagsView/delView", this.$route); //關閉當前頁 this.$router.replace({ path: "/xxx/xxx"…