2026.9.11

74 搜索二维矩阵

python 复制代码
class Solution:
    def searchMatrix(self, matrix: List[List[int]], target: int) -> bool:
        m,n=len(matrix),len(matrix[0])
        left=-1
        right=m*n
        while left+1<right:
            mid=(left+right)//2
            x=matrix[mid//n][mid%n]
            if target==x:return True
            if x<target:left=mid
            if x>target:right=mid
        return False

34 在排序数组中查找元素的第一个和最后一个位置

python 复制代码
class Solution:
    def searchRange(self, nums: List[int], target: int) -> List[int]:
        def lower_bound(nums: List[int], target: int) -> List[int]:
            left,right=-1,len(nums)
            while left+1<right:
                mid=(left+right)//2
                x=nums[mid]
                if x<target:left=mid
                else:right=mid
            return right
        
        start=lower_bound(nums,target)
        if start==len(nums) or nums[start]!=target:return [-1,-1]
        end=lower_bound(nums,target+1)-1
        return [start,end]
相关推荐
“AI国潮设计-小江”7 小时前
【AIGC实战】Python+SDXL批量生成“潮汕英歌舞不倒翁”IP | 角色一致性与多场景融合方案,附完整代码
开发语言·人工智能·python·prompt·aigc
Linux_cy7 小时前
服务器管理工具哪个好用?我用了七八个
运维·服务器·javascript·python·ssh·面板·gmssh
MoRanzhi12037 小时前
第6篇(下)· 使用 PyQt5 实现 2048 高性能计算版:统一运行控制与跨模式安全交接
python·pyqt·进程管理·状态机·软件架构·并发控制·数据一致性
code tsunami7 小时前
【无标题】
开发语言·python
春弦_7 小时前
自用算法整理二
数据结构·算法·图论
CodeGL7 小时前
10、Python基础之进程和线程
开发语言·python
hetao17338378 小时前
2026-10-22~23 hetao1733837 的刷题记录
c++·算法
TechWayfarer8 小时前
PHP漏洞被利用,怎么揪出攻击IP?
开发语言·python·tcp/ip·网络安全·php
java_nnnn9 小时前
LeetCode刷题滑动窗口(串联所有单词的子串+最小覆盖的子串)Java
算法·leetcode·职场和发展
2601_967980119 小时前
Python列表操作方法详解
python·数据类型·修改·列表操作·功能汇总