日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

【LeetCode】3月23日打卡-Day8

發布時間:2024/7/5 44 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【LeetCode】3月23日打卡-Day8 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題1 鏈表的中間結點

描述

給定一個帶有頭結點 head 的非空單鏈表,返回鏈表的中間結點。
如果有兩個中間結點,則返回第二個中間結點。
示例 1:
輸入:[1,2,3,4,5]
輸出:此列表中的結點 3 (序列化形式:[3,4,5])
返回的結點值為 3 。 (測評系統對該結點序列化表述是 [3,4,5])。
注意,我們返回了一個 ListNode 類型的對象 ans,這樣:
ans.val = 3, ans.next.val = 4, ans.next.next.val = 5, 以及 ans.next.next.next = NULL.
示例 2:
輸入:[1,2,3,4,5,6]
輸出:此列表中的結點 4 (序列化形式:[4,5,6])
由于該列表有兩個中間結點,值分別為 3 和 4,我們返回第二個結點。
提示:
給定鏈表的結點數介于 1 和 100 之間。

題解1 雙指針法

思路:用兩個指針,p是慢指針,q是快指針,移動速度是p的兩倍,當q到達末尾的時候,p指針正好指在鏈表中間。

class Solution {public ListNode middleNode(ListNode head) {ListNode q = head;ListNode p = head;while(q != null && q.next != null){q = q.next.next;p = p.next;}return ;} }

題解2 單指針法

思路:首先遍歷鏈表,得到鏈表長度,然后遍歷至鏈表長度二分之一處

class Solution {public ListNode middleNode(ListNode head) {int n = 0;ListNode cur = head;while (cur != null) {++n;cur = cur.next;}int k = 0;cur = head;while (k < n / 2) {++k;cur = cur.next;}return cur;} }

題解3 數組法

思路:鏈表的缺點在于不能通過下標訪問對應的元素。因此我們可以考慮對鏈表進行遍歷,同時將遍歷到的元素依次放入數組 A 中。如果我們遍歷到了 N 個元素,那么鏈表以及數組的長度也為 N,對應的中間節點即為 A[N/2]。

class Solution {public ListNode middleNode(ListNode head) {ListNode[] A = new ListNode[100];int t = 0;while (head != null) {A[t++] = head;head = head.next;}return A[t / 2];} }

題2 環形鏈表

描述

給定一個鏈表,判斷鏈表中是否有環。
為了表示給定鏈表中的環,我們使用整數 pos 來表示鏈表尾連接到鏈表中的位置(索引從 0 開始)。 如果 pos 是 -1,則在該鏈表中沒有環。
示例 1:
輸入:head = [3,2,0,-4], pos = 1
輸出:true
解釋:鏈表中有一個環,其尾部連接到第二個節點。
示例 2:
輸入:head = [1,2], pos = 0
輸出:true
解釋:鏈表中有一個環,其尾部連接到第一個節點。
示例 3:
輸入:head = [1], pos = -1
輸出:false
解釋:鏈表中沒有環。

題解1 快慢指針法

思路:如果存在環,快慢指針總會相遇。

/*** Definition for singly-linked list.* class ListNode {* int val;* ListNode next;* ListNode(int x) {* val = x;* next = null;* }* }*/ public class Solution {public boolean hasCycle(ListNode head) {ListNode i = head;ListNode j = head;while(i != null && i.next != null){i = i.next.next;j = j.next;if(i == j){return true;}}return false;} }

題解2 哈希表法

思路:通過檢查一個結點此前是否被訪問過來判斷鏈表是否為環形鏈表。常用的方法是使用哈希表。我們遍歷所有結點并在哈希表中存儲每個結點的引用(或內存地址)。如果當前結點為空結點 null(即已檢測到鏈表尾部的下一個結點),那么我們已經遍歷完整個鏈表,并且該鏈表不是環形鏈表。如果當前結點的引用已經存在于哈希表中,那么返回 true(即該鏈表為環形鏈表)。

public boolean hasCycle(ListNode head) {Set<ListNode> nodesSeen = new HashSet<>();while (head != null) {if (nodesSeen.contains(head)) {return true;} else {nodesSeen.add(head);}head = head.next;}return false; }

題3 實現strStr()

描述

題解1

思路:如果子串為空,返回0。如果子串長于原字符串剩余字符串,則找不到,返回-1。如果原字符串當前位置和子串首字母匹配,則對照字符串依次匹配字符,如果子串全匹配,返回i,否則退出子串的循環,原字符串當前位置向后移動。

class Solution {public int strStr(String haystack, String needle) {if(needle.length() == 0){return 0;}for(int i = 0; i < haystack.length(); i++){if ((haystack.length() - i) < needle.length()) {return -1;}if(haystack.charAt(i) == needle.charAt(0)){boolean flag = true;for(int j = 0; j < needle.length(); j++){if(haystack.charAt(i+j) != needle.charAt(j)){flag = false;}}if(flag){return i;}}}return -1;} }

題解2 滑動窗口+subString

class Solution {public int strStr(String haystack, String needle) {int L = needle.length(), n = haystack.length();for (int start = 0; start < n - L + 1; ++start) {if (haystack.substring(start, start + L).equals(needle)) {return start;}}return -1;} }

題解3 雙指針法

思路:pn指向原字符串,pL指向子串,當在原字符串中找到匹配子串第一個字母時,比較兩字符串,記錄相同的字符數,遇到不同的字符即退出比較循環,如果相同字符數等于子串字符數,返回pn減去子串長度(原串和子串在一起移動)。如果相同字符數不等于子串字符數,說明匹配失敗,原字符串要回溯到比較循環之前的位置,即pn-子串長度+1位置。

class Solution {public int strStr(String haystack, String needle) {int L = needle.length(), n = haystack.length();if (L == 0) return 0;int pn = 0;while (pn < n - L + 1) {// find the position of the first needle character// in the haystack stringwhile (pn < n - L + 1 && haystack.charAt(pn) != needle.charAt(0)) ++pn;// compute the max match stringint currLen = 0, pL = 0;while (pL < L && pn < n && haystack.charAt(pn) == needle.charAt(pL)) {++pn;++pL;++currLen;}// if the whole needle string is found,// return its start positionif (currLen == L) return pn - L;// otherwise, backtrackpn = pn - currLen + 1;}return -1;} }

總結

以上是生活随笔為你收集整理的【LeetCode】3月23日打卡-Day8的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。