LeetCode騰訊50題-Day13-160/169/206
阿新 • • 發佈:2021-01-25
技術標籤:LeetCode資料結構python連結串列python資料結構演算法leetcode
LeetCode50題(17天)-Day13
160 相交連結串列
- 題號:160
- 難度:簡單
- https://leetcode-cn.com/problems/intersection-of-two-linked-lists/
編寫一個程式,找到兩個單鏈表相交的起始節點。
如下面的兩個連結串列:
在節點 c1 開始相交。
示例 1:
輸入:intersectVal = 8, listA = [4,1,8,4,5], listB = [5,0,1,8,4,5], skipA = 2, skipB = 3
輸出:Reference of the node with value = 8
輸入解釋:相交節點的值為 8 (注意,如果兩個列表相交則不能為 0)。
從各自的表頭開始算起,連結串列 A 為 [4,1,8,4,5],連結串列 B 為 [5,0,1,8,4,5]。
在 A 中,相交節點前有 2 個節點;在 B 中,相交節點前有 3 個節點。
示例 2:
輸入:intersectVal = 2, listA = [0,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1
輸出:Reference of the node with value = 2
輸入解釋:相交節點的值為 2 (注意,如果兩個列表相交則不能為 0)。
從各自的表頭開始算起,連結串列 A 為 [ 0,9,1,2,4],連結串列 B 為 [3,2,4]。
在 A 中,相交節點前有 3 個節點;在 B 中,相交節點前有 1 個節點。
示例 3:
輸入:intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2
輸出:null
輸入解釋:從各自的表頭開始算起,連結串列 A 為 [2,6,4],連結串列 B 為 [1,5]。
由於這兩個連結串列不相交,所以 intersectVal 必須為 0,而 skipA 和 skipB 可以是任意值。
解釋:這兩個連結串列不相交,因此返回 null。
注意:
- 如果兩個連結串列沒有交點,返回 null.
- 在返回結果後,兩個連結串列仍須保持原有的結構。
- 可假定整個連結串列結構中沒有迴圈。
- 程式儘量滿足 O(n) 時間複雜度,且僅用 O(1) 記憶體。
實現
思路:通過集合的方法
C# 語言
- 狀態:通過
- 45 / 45 個通過測試用例
- 執行用時: 172 ms, 在所有 C# 提交中擊敗了 100.00% 的使用者
- 記憶體消耗: 37.6 MB, 在所有 C# 提交中擊敗了 5.88% 的使用者
/**
* Definition for singly-linked list.
* public class ListNode {
* public int val;
* public ListNode next;
* public ListNode(int x) { val = x; }
* }
*/
public class Solution
{
public ListNode GetIntersectionNode(ListNode headA, ListNode headB)
{
HashSet<ListNode> hash = new HashSet<ListNode>();
ListNode temp = headA;
while (temp != null)
{
hash.Add(temp);
temp = temp.next;
}
temp = headB;
while (temp != null)
{
if (hash.Contains(temp))
return temp;
temp = temp.next;
}
return null;
}
}
Python 語言
- 執行結果:通過
- 執行用時:200 ms, 在所有 Python3 提交中擊敗了 40.19% 的使用者
- 記憶體消耗:29.4 MB, 在所有 Python3 提交中擊敗了 5.00% 的使用者
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> None:
h = set()
temp = headA
while temp is not None:
h.add(temp)
temp = temp.next
temp = headB
while temp is not None:
if temp in h:
return temp
temp = temp.next
return None
169 多數元素
- 題號:169
- 難度:簡單
- https://leetcode-cn.com/problems/majority-element/
給定一個大小為 n
的陣列,找到其中的眾數。眾數是指在陣列中出現次數大於⌊ n/2 ⌋
的元素。
你可以假設陣列是非空的,並且給定的陣列總是存在眾數。
示例 1:
輸入: [3,2,3]
輸出: 3
示例 2:
輸入: [2,2,1,1,1,2,2]
輸出: 2
實現
第一種:利用排序的方法
C# 語言
- 狀態:通過
- 44 / 44 個通過測試用例
- 執行用時:192 ms
public class Solution
{
public int MajorityElement(int[] nums)
{
nums = nums.OrderBy(a => a).ToArray();
return nums[nums.Length / 2];
}
}
Python 語言
- 執行結果:通過
- 執行用時:48 ms, 在所有 Python3 提交中擊敗了 82.08% 的使用者
- 記憶體消耗:15.2 MB, 在所有 Python3 提交中擊敗了 6.90% 的使用者
class Solution:
def majorityElement(self, nums: List[int]) -> int:
nums.sort()
return nums[len(nums) // 2]
第二種:利用 Boyer-Moore 投票演算法
尋找陣列中超過一半的數字,這意味著陣列中其他數字出現次數的總和都是比不上這個數字出現的次數。即如果把 該眾數記為 +1 ,把其他數記為 −1 ,將它們全部加起來,和是大於 0 的。
- 狀態:通過
- 44 / 44 個通過測試用例
- 執行用時:148 ms
public class Solution
{
public int MajorityElement(int[] nums)
{
int candidate = nums[0];
int count = 1;
for (int i = 1; i < nums.Length; i++)
{
if (count == 0)
candidate = nums[i];
count += (nums[i] == candidate) ? 1 : -1;
}
return candidate;
}
}
206 反轉連結串列
- 題號:206
- 難度:簡單
- https://leetcode-cn.com/problems/reverse-linked-list/
反轉一個單鏈表。
示例:
輸入: 1->2->3->4->5->NULL
輸出: 5->4->3->2->1->NULL
進階:
你可以迭代或遞迴地反轉連結串列。你能否用兩種方法解決這道題?
實現
思路:利用雙指標的方式
p1
作為前面的指標探路,p2
作為後面的指標跟進,順著連結串列跑一圈,搞定問題。
C# 語言
- 狀態:通過
- 27 / 27 個通過測試用例
- 執行用時: 116 ms, 在所有 C# 提交中擊敗了 97.50% 的使用者
- 記憶體消耗: 23.3 MB, 在所有 C# 提交中擊敗了 5.26% 的使用者
/**
* Definition for singly-linked list.
* public class ListNode {
* public int val;
* public ListNode next;
* public ListNode(int x) { val = x; }
* }
*/
public class Solution
{
public ListNode ReverseList(ListNode head)
{
if (head == null || head.next == null)
return head;
ListNode p1 = head;
ListNode p2 = null;
while (p1 != null)
{
ListNode temp = p1.next;
p1.next = p2;
p2 = p1;
p1 = temp;
}
return p2;
}
}
Python 語言
- 執行結果:通過
- 執行用時:36 ms, 在所有 Python3 提交中擊敗了 92.27% 的使用者
- 記憶體消耗:14.6 MB, 在所有 Python3 提交中擊敗了 17.65% 的使用者
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def reverseList(self, head: ListNode) -> ListNode:
if head is None or head.next is None:
return head
p1 = head
p2 = None
while p1 is not None:
temp = p1.next
p1.next = p2
p2 = p1
p1 = temp
return p2