原题
https://leetcode-cn.com/problems/search-a-2d-matrix/

思路
一次二分查找,判断mid所在的横纵位置i=mid/col,j=mid%col
题解
package com.leetcode.code;
/**
* @Description:
* @ClassName: Code74
* @Author: ZK
* @Date: 2021/3/30 22:14
* @Version: 1.0
*/
public class Code74 {
public static void main(String[] args) {
int[][] matrix = {{1,3,5,7},{10,11,16,20},{23,30,34,60}};
int target = 12;
boolean res = searchMatrix(matrix, target);
System.out.println(res);
}
public static boolean searchMatrix(int[][] matrix, int target) {
int row = matrix.length;
int col = matrix[0].length;
int len = row * col;
int low = 0;
int high = len - 1;
while(low <= high) {
int mid = (high + low)/2;
int cur = matrix[mid/col][mid%col];
if (target > cur) {
low = mid + 1;
} else if (target < cur) {
high = mid - 1;
} else {
return true;
}
}
return false;
}
}

本文介绍如何使用二分查找策略解决LeetCode题目74——在一个二维矩阵中查找目标元素。通过实例展示了如何在给定矩阵中找到目标值的逻辑,适合初学者理解搜索算法在实际问题中的应用。
606

被折叠的 条评论
为什么被折叠?



