力扣 搜索二维矩阵 二分

‍ 搜索二维矩阵


力扣 搜索二维矩阵 二分_第1张图片
力扣 搜索二维矩阵 二分_第2张图片

✨ AC code

class Solution {
   public boolean searchMatrix(int[][] matrix, int target)
	{
		int l = 0;
		int row = matrix.length;
		int col = matrix[0].length;
		int r = row * col - 1;
		while (l < r)
		{
			int m = l + r >> 1;
			int x = m / col;
			int y = m % col;
			if (matrix[x][y] >= target)
				r = m;
			else
				l = m + 1;
		}
		if (matrix[l / col][l % col] == target)
			return true;
		return false;
	}
}

你可能感兴趣的:(#,力扣,hot100,leetcode,矩阵,算法)