前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >74. 搜索二维矩阵

74. 搜索二维矩阵

作者头像
张伦聪zhangluncong
发布2022-10-26 17:56:51
1750
发布2022-10-26 17:56:51
举报

编写一个高效的算法来判断 m x n 矩阵中,是否存在一个目标值。该矩阵具有如下特性:

每行中的整数从左到右按升序排列。 每行的第一个整数大于前一行的最后一个整数。 示例 1:

代码语言:javascript
复制
输入:
matrix = [
  [1,   3,  5,  7],
  [10, 11, 16, 20],
  [23, 30, 34, 50]
]
target = 3
输出: true

示例 2:

代码语言:javascript
复制
输入:
matrix = [
  [1,   3,  5,  7],
  [10, 11, 16, 20],
  [23, 30, 34, 50]
]
target = 13
输出: false

解:第一次二分法判断在哪一行,第二次二分法判断在哪一列。注意二分法,不进行mid加减1,while不加=,容易在特殊情况出现死循环。

代码语言:javascript
复制
public boolean searchMatrix(int[][] matrix, int target) {
        if (matrix.length == 0 || matrix[0].length == 0)
            return false;
        if (target < matrix[0][0] || target > matrix[matrix.length - 1][matrix[0].length - 1])
            return false;
        int rowNum = 0;
        //二分法搜索第一列,判断target在哪一行
        int top = 0;
        int bottom = matrix.length - 1;
        while (top <= bottom) {
            int mid = (top + bottom) / 2;
            if (matrix[mid][0] > target) {
                bottom = mid - 1;
            } else if (matrix[mid][0] < target) {
                top = mid + 1;
            } else {
                return true;
            }
        }
        rowNum = top - 1;

        //二分法搜索行,判断在哪一列
        int left = 0;
        int right = matrix[0].length - 1;
        while (left <= right) {
            int mid = (left + right) / 2;
            if (matrix[rowNum][mid] > target) {
                right = mid - 1;
            } else if (matrix[rowNum][mid] < target) {
                left = mid + 1;
            } else {
                return true;
            }
        }
        return false;
    }
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018-06-09,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档