240. 搜索二维矩阵 II

240. 搜索二维矩阵 II

原题链接:

240. 搜索二维矩阵 II

https://leetcode.cn/problems/search-a-2d-matrix-ii/description/

完成情况:

解题思路:

从右上角开始判断走迷宫,效果最佳。!!!

参考代码:

java 复制代码
package 西湖算法题解___中等题;

public class __240搜索二维矩阵II__检测移动 {
	public boolean searchMatrix(int[][] matrix, int target) {
		/*
		从第一行最右边开始可以最快的锁定是否应该在哪一行
		 */
		int col  = matrix.length;
		int row = matrix[0].length;
		int x = 0,y=row-1;
		while (x<col && y>= 0){     //一个监控上下,一个监控左右
			if (matrix[x][y] == target){
				return true;
			}
			if (matrix[x][y] > target){
				y--;
			}else {
				x++;
			}
		}
		return false;
	}
}
相关推荐
vvilkim1 小时前
数据结构类型、区别与应用场景:一篇讲透选型逻辑
数据结构
文丑颜不良啊2 小时前
LeetCode2461~2480题目解析
java·leetcode
文丑颜不良啊3 小时前
LeetCode2441~2460题目解析
java·leetcode
文丑颜不良啊4 小时前
LeetCode2521~2540题目解析
java·leetcode
lisw055 小时前
Dayhoff的PAM替换矩阵理论评介!
线性代数·算法·矩阵
阿卡好可爱5 小时前
Lua 热更新不生效:改完文件线上仍是旧逻辑,package.loaded 与 upvalue 重绑定详解
数据结构·分布式·后端·架构·lua
一起学安全技术6 小时前
拆大模型 03|注意力的一句话版本:不用论文讲清 Attention
深度学习·算法·语言模型·transformer
Delightrs7 小时前
Rust LeetCode238 算法题解
开发语言·算法·rust
自信150413057597 小时前
重生之从0开始学习c++之——vector-中篇
c++·学习·算法
文丑颜不良啊7 小时前
LeetCode2501~2520题目解析
java·leetcode