力扣 240.搜索二维矩阵II

文章目录

题目介绍

题解

代码如下:

java 复制代码
class Solution {
    public boolean searchMatrix(int[][] matrix, int target) {
        int i = 0;
        int j = matrix[0].length - 1; // 从右上角开始
        while (i < matrix.length && j >= 0) { // 还有剩余元素
            if (matrix[i][j] == target) {
                return true; // 找到 target
            }
            if (matrix[i][j] < target) {
                i++; // 这一行剩余元素全部小于 target,排除
            } else {
                j--; // 这一列剩余元素全部大于 target,排除
            }
        }
        return false;
    }
}
相关推荐
code2cat1 小时前
【随笔】Java客户端选了HTTP/3,怎样知道请求实际走了哪个版本
java·开发语言·http
码云社区2 小时前
Java在线租房系统源码:VR看房素材、房源图片与内容维护方案
java·vue.js·vr·租房小程序·在线租房·看房租房
码云社区3 小时前
Spring Boot智慧物业系统源码:合同到期提醒、续签资料与资产协作
java·spring boot·后端·物业管理·物业系统·智慧物业
AI开源软件3 小时前
Vue3+SpringBoot3 业务配置统一管理:接管、保存与回退怎样保留有效值
java·前端·vue.js·spring boot·vue·ruoyi office
陈如水3 小时前
Spring AI Alibaba 在多 Agent 场景下的实践
java·人工智能·spring
吃我一窝jio4 小时前
Docker+Jenkins+K8s全流程部署
java·开发语言·笔记·python
Resean02237 小时前
Spring 源码分析(六)循环依赖与三级缓存
java·spring·缓存
在爪洼山玩泥巴7 小时前
fastjson2 JSONPath 取不存在路径,为什么会抛 “not support”?
java·开发语言·jsonpath·fastjson
星落梦生17 小时前
SpringAOP基础笔记(7)
java·开发语言
金銀銅鐵18 小时前
带您看懂字节码指令(四):一条指令里有什么
java·jvm