【代码】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
相关推荐
伞伞悦读42 分钟前
【第14期】Python 基础语法入门:缩进、注释、标识符、代码块和解释器执行
python
Java后端的Ai之路1 小时前
03、Python - 抽象工厂模式
开发语言·python·设计模式·抽象工厂模式·通用智慧
GGG7661 小时前
Python 运维自动化:psutil+IPy 系统与网络信息采集深度实战
运维·python·云原生·pip
可乐鸡翅yeah_1 小时前
个人 / 外包流媒体项目交付实战,M3U8 验收与问题闭环完整工作流
运维·网络·python·低代码·django·m3u8·在线播放
人机(未转人工)2 小时前
python 批量测试ip是否能ping( 多线程 )
python
cndes2 小时前
安装Miniconda+Jupyter notebook
ide·python·jupyter
Patrick在香港2 小时前
Python 拉取城巴开放数据:406 条路线,香港岛巴士网络的数字画像
网络·python·数据分析·php·数据可视化·香港·开放数据
for_ever_love__2 小时前
python基础语法学习: 正则表达式
python·学习·正则表达式
aiqianji2 小时前
功能全面的教AI短篇小说写作的软件都有哪些?
人工智能·python
轮到我狗叫了3 小时前
Slurm如何使用
人工智能·python·深度学习·机器学习