LeetCode實戰:環形鏈表


LeetCode實戰:環形鏈表


題目英文

Given a linked list, determine if it has a cycle in it.

To represent a cycle in the given linked list, we use an integer pos which represents the position (0-indexed) in the linked list where tail connects to. If pos is -1, then there is no cycle in the linked list.

Example 1:

Input: head = [3,2,0,-4], pos = 1
Output: true
Explanation: There is a cycle in the linked list, where tail connects to the second node.
LeetCode實戰:環形鏈表

Example 2:

Input: head = [1,2], pos = 0
Output: true
Explanation: There is a cycle in the linked list, where tail connects to the first node.
LeetCode實戰:環形鏈表

Example 3:

Input: head = [1], pos = -1
Output: false
Explanation: There is no cycle in the linked list.
LeetCode實戰:環形鏈表

Follow up:

Can you solve it using O(1) (i.e. constant) memory?

題目中文

給定一個鏈表,判斷鏈表中是否有環。

為了表示給定鏈表中的環,我們使用整數pos 來表示鏈表尾連接到鏈表中的位置(索引從 0 開始)。如果pos是 -1,則在該鏈表中沒有環。

示例 1

輸入:head = [3,2,0,-4], pos = 1
輸出:true
解釋:鏈表中有一個環,其尾部連接到第二個節點。
LeetCode實戰:環形鏈表

示例 2

輸入:head = [1,2], pos = 0
輸出:true
解釋:鏈表中有一個環,其尾部連接到第一個節點。
LeetCode實戰:環形鏈表

示例 3

輸入:head = [1], pos = -1
輸出:false
解釋:鏈表中沒有環。
LeetCode實戰:環形鏈表

進階

你能用 O(1)(即,常量)內存解決此問題嗎?

算法實現

/**
 * Definition for singly-linked list.
 * public class ListNode {
 * public int val;
 * public ListNode next;
 * public ListNode(int x) {
 * val = x;
 * next = null;
 * }
 * }
 */
 // 通過檢查一個結點此前是否被訪問過來判斷鏈表是否為環形鏈表。
public class Solution {
 public bool HasCycle(ListNode head) {
 HashSet hashSet = new HashSet();
 hashSet.Add(head);
 while (head != null)
 {
 head = head.next;
 if (head == null)
 return false;
 if (hashSet.Contains(head))
 return true;
 hashSet.Add(head);
 }
 return false; 
 }
}

實驗結果

  • 狀態:通過
  • 17 / 17 個通過測試用例
  • 執行用時:144 ms
LeetCode實戰:環形鏈表

提交結果

相關圖文

  • LeetCode實戰:刪除鏈表的倒數第N個節點
  • LeetCode實戰:合併兩個有序鏈表
  • LeetCode實戰:兩兩交換鏈表中的節點
  • LeetCode實戰:旋轉鏈表
  • LeetCode實戰:相同的樹
  • LeetCode實戰:對稱二叉樹
  • LeetCode實戰:二叉樹的最大深度
  • LeetCode實戰:搜索二維矩陣
  • LeetCode實戰:將有序數組轉換為二叉搜索樹
  • 資料分享:數學建模資料分享 -- 圖論部分
  • 資料分享:數學建模資料分享 -- 神經網絡部分
  • 如何利用 C# 實現 K 最鄰近算法?
  • 如何利用 C# 實現 K-D Tree 結構?
  • 如何利用 C# + KDTree 實現 K 最鄰近算法?
  • 如何利用 C# 對神經網絡模型進行抽象?
  • 如何利用 C# 實現神經網絡的感知器模型?
  • 如何利用 C# 實現 Delta 學習規則?
  • 如何利用 C# 實現 誤差反向傳播 學習規則?
  • 如何利用 C# 爬取帶 Token 驗證的網站數據?
  • 如何利用 C# 向 Access 數據庫插入大量數據?
  • 如何利用 C# + Python 破解貓眼電影的反爬蟲機制?

經過8年多的發展,LSGO軟件技術團隊在「地理信息系統」、「數據統計分析」、「計算機視覺」等領域積累了豐富的研發經驗,也建立了人才培養的完備體系,由於自己準備在「量化交易」領域精進技能,如果大家對這個領域感興趣可以與我聯繫,加入我們的量化學習群一起學習探討。

在這個領域我已做了以下積累:

策略部分:

  • 數字貨幣 One 的投資價值分析
  • 數字資產量化中的跨市場套利策略
  • 數字資產量化中的同市場套利策略
  • 數字資產量化中的網格交易法
  • 我們能否效仿李笑來的投資策略?
  • 賺錢是剛需,如何正確的交易股票?

數據部分:

  • 如何利用 C# 爬取 One 的交易數據?
  • 如何利用 C# 爬取 One 持有者返利數據?
  • 如何利用 C# 爬取BigOne交易所的公告?
  • 如何利用 C# 爬取Gate.io交易所的公告?
  • 如何利用 C# 爬取「財報說」中的股票數據?

自動化交易部分:

  • 封裝BigOne API:身份驗證
  • 封裝BigOne API:獲取賬戶資產
  • 封裝BigOne API:訂單系統
  • 封裝BigOne API:網格交易法
  • 封裝BigOne API:代碼的重構
  • 進一步完善自動化交易系統 01
  • 進一步完善自動化交易系統 02
  • 進一步完善自動化交易系統 03
  • 進一步完善自動化交易系統 04
  • 如何開發「股票數據分析軟件」(上)
  • 如何開發「股票數據分析軟件」(中)
  • 如何開發「股票數據分析軟件」(下)
  • 進一步完善「股票數據分析軟件」 - 01

後臺回覆「搜搜搜」,隨機獲取電子資源!

歡迎關注,請掃描二維碼:

LeetCode實戰:環形鏈表



分享到:


相關文章: