🔥 LeetCode 热题 HOT 100 - 力扣(LeetCode)
从刷HOT 100的简单题和中等题开始
第一题:两数之和

class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
numlen = len(nums)
for i in range(numlen):
for j in range(i+1,numlen):
if nums[i]+nums[j]==target:
return [i,j]
return []
第二题:有效的括号

class Solution:
def isValid(self, s: str) -> bool:
while '()' in s or '[]' in s or '{}' in s:
s = s.replace ('()','')
s = s.replace ('[]','')
s = s.replace ('{}','')
return s==''
0202我又来了
第三题:合并两个有序链表

class Solution:
def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode:
if l1 is None:
return l2
elif l2 is None:
return l1
if l1.val<l2.val:
l1.next = self.mergeTwoLists(l1.next,l2)
return l1
else:
l2.next = self.mergeTwoLists(l2.next,l1)
return l2
本文解析了LeetCode热题HOT100中的三道经典题目:两数之和、有效的括号及合并两个有序链表。通过Python实现,展示了算法设计的基本思路和技术要点。

被折叠的 条评论
为什么被折叠?



