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