当前位置:   article > 正文

240. 搜索二维矩阵 II

240. 搜索二维矩阵 II

原题链接:

240. 搜索二维矩阵 II

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

完成情况:

在这里插入图片描述

解题思路:

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

参考代码:

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;
	}
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/282861
推荐阅读
相关标签
  

闽ICP备14008679号