【leetcode】2021-1-31 天线宝宝又开始刷题了

本文解析了LeetCode热题HOT100中的三道经典题目:两数之和、有效的括号及合并两个有序链表。通过Python实现,展示了算法设计的基本思路和技术要点。

🔥 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        

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值