【代码】hot100

Easy

两数之和

两数之和

复制代码
class Solution:
    def twoSum(self, nums: List[int], target: int) -> List[int]:
        xdict={}
        for i in range(len(nums)):
            j=target-nums[i]
            if j in xdict.keys():
                return [i,xdict[j]]
            else:
                xdict[nums[i]]=i        

有效的括号

有效的括号

复制代码
class Solution:
    def isValid(self, s: str) -> bool:
        map_dict={
            ')':'(',
            '}':'{',
            ']':'[',
        }
        st=[]
        for item in s:
            if item not in map_dict.keys():
                st.append(item)
            else:
                if not st:
                    return False
                pop_item=st.pop()
                if pop_item!=map_dict[item]:    
                    # 如果是嵌套括号的话,不能交错嵌套。如果不是交错嵌套的,内层的会正确被 pop 出来,所以只需要 pop 最后的那个就可以了。
                    return False
        return not st

合并两个有序链表

合并两个有序链表

复制代码
# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def mergeTwoLists(self, list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]:
        # if not list1 and not list2:
        #     return None
        curr=ListNode()
        head=curr
        while list1 and list2:
            if list1.val<list2.val:
                curr.next=list1
                list1=list1.next
            else:
                curr.next=list2
                list2=list2.next
            curr=curr.next
        if list1:
            curr.next=list1
        if list2:
            curr.next=list2
        return head.next

爬楼梯

爬楼梯

复制代码
class Solution:
    def climbStairs(self, n: int) -> int:
        if n==1:
            return 1
        if n==2:
            return 2
        dp=[0]*n
        dp[0]=1
        dp[1]=2
        for i in range(2,n):
            dp[i]=dp[i-1]+dp[i-2]
        return dp[n-1]

二叉树的中序遍历

二叉树的中序遍历

复制代码
# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def inorderTraversal(self, root: Optional[TreeNode]) -> List[int]:

        ret=[]
        def dfs(node):
            if not node:
                return
            dfs(node.left)
            ret.append(node.val)
            dfs(node.right)
        dfs(root)
        return ret

对称二叉树

对称二叉树

复制代码
# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def isSymmetric(self, root: Optional[TreeNode]) -> bool:
        if not root:
            return True

        def search(t1,t2):
            if not t1 and not t2:
                return True
            if not t1 or not t2:
                return False
            return t1.val==t2.val and search(t1.left,t2.right) and search(t1.right,t2.left)
        return search(root.left,root.right)

二叉树的最大深度

二叉树的最大深度

复制代码
# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def maxDepth(self, root: Optional[TreeNode]) -> int:
        if not root:
            return 0
        left_dep=self.maxDepth(root.left)
        right_dep=self.maxDepth(root.right)
        return max(left_dep,right_dep)+1
        # 加一的根本原因是:为了把当前节点所在的这一层计算进去。

买卖股票的最佳时机

买卖股票的最佳时机

复制代码
class Solution:
    def maxProfit(self, prices: List[int]) -> int:
        max_ret=0
        min_cost=float('inf')    
        n=len(prices)
        for i in range(1,n):
            min_cost=min(min_cost,prices[i-1])
            max_ret=max(max_ret,prices[i]-min_cost)    
        return max_ret
相关推荐
神也畏惧菠萝14 分钟前
Day08 Python流程控制从入门到上瘾:if/match/while/for四件套,从B站登录一路打到猜数字游戏
python·算法·数据存储·for·while·if·ifelse
言乐628 分钟前
通过 3D 动画直观演示球形瓶内可滑动半圆机构在倾倒时防粘盖、回正时刮膏体的运动学过程
python·3d·django·virtualenv·pygame
quantdash_cc35 分钟前
ETF 行情数据清洗实战:字段语义拆解、标的池整理与五个校验清单
python·数据分析·pandas·量化交易·股票数据·quantdash
小熊恋旧37 分钟前
Python 面向对象编程:方法、属性、继承与多态详解
开发语言·python
威哥爱编程1 小时前
Python 智能体开发实战 05:一问教材内容它就编?先把答案从你自己的资料里检索出来
python·agent
java_prinln1 小时前
RAG 面试题介绍
python·agent
威哥爱编程1 小时前
Python 智能体开发实战 06:一个函数里塞了八百行?用状态机把批改流程拆成能回退的步骤
python·agent
Patrick在香港2 小时前
一个斜杠,把毫秒变成十几秒
android·数据库·python·性能优化·正则表达式·回溯·redos
2601_967980112 小时前
Python 初学者必备的常用内置函数
python·编程语言·内置函数·初学者·常用函数
OnlineProxy3 小时前
移动应用数据爬取:通过模拟器与流量嗅探对 API 进行逆向工程
爬虫·python·httpx