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;
	}
}
相关推荐
wadesir6 小时前
Rust中的条件变量详解(使用Condvar的wait方法实现线程同步)
开发语言·算法·rust
yugi9878387 小时前
基于MATLAB实现协同过滤电影推荐系统
算法·matlab
TimberWill7 小时前
哈希-02-最长连续序列
算法·leetcode·排序算法
Morwit7 小时前
【力扣hot100】64. 最小路径和
c++·算法·leetcode
leoufung7 小时前
LeetCode 373. Find K Pairs with Smallest Sums:从暴力到堆优化的完整思路与踩坑
java·算法·leetcode
wifi chicken8 小时前
数组遍历求值,行遍历和列遍历谁更快
c语言·数据结构·算法
qingyun9898 小时前
深度优先遍历:JavaScript递归查找树形数据结构中的节点标签
前端·javascript·数据结构
胡楚昊8 小时前
NSSCTF动调题包通关
开发语言·javascript·算法
Gold_Dino9 小时前
agc011_e 题解
算法
bubiyoushang8889 小时前
基于蚁群算法的直流电机PID参数整定 MATLAB 实现
数据结构·算法·matlab