LeetCode HOT 100
Posted sandy-t
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LeetCode HOT 100相关的知识,希望对你有一定的参考价值。
记录
I
通过:1,
错误:206(递归返回条件和边界条件),
1.两数之和-简单
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
lookup = {}
for i in range(len(nums)):
num = nums[i]
if target - num in lookup:
return [i, lookup[target - num]]
else:
lookup[num] = i
206.反转链表-简单
# 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:
"""
迭代1:
pre,1 cur,2,3,4,None
pre,2,1 cur,3,4,None
if head is None or head.next is None:
return head
pre = ListNode(None)
pre.next = head
cur = head
while cur.next:
tmp = cur.next
cur.next = tmp.next
tmp.next = pre.next
pre.next = tmp
return pre.next
"""
"""
递归:
# 1,2,3,4,None
# reverse(2,3,4),1,None
if head is None or head.next is None: ## 易错点:head为空; 返回条件:递归到最后一个非空节点
return head
node = self.reverseList(head.next)
head.next.next = head
head.next = None
return node
"""
以上是关于LeetCode HOT 100的主要内容,如果未能解决你的问题,请参考以下文章
LeetCode Hot 100 --- 括号生成(java图解超级详细)
#yyds干货盘点# LeetCode 热题 HOT 100:子集