侧边栏壁纸
博主头像
GabrielxD

列車は必ず次の駅へ。では舞台は?私たちは?

  • 累计撰写 675 篇文章
  • 累计创建 128 个标签
  • 累计收到 22 条评论

目 录CONTENT

文章目录

【暴力, 二分查找, Z形查找】搜索二维矩阵 II

GabrielxD
2022-04-17 / 0 评论 / 0 点赞 / 239 阅读 / 586 字
温馨提示:
本文最后更新于 2023-09-12,若内容或图片失效,请留言反馈。部分素材来自网络,若不小心影响到您的利益,请联系我们删除。

题目

240. 搜索二维矩阵 II


编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target 。该矩阵具有以下特性:

  • 每行的元素从左到右升序排列。
  • 每列的元素从上到下升序排列。

示例 1:

image-20220416231655618

输入:matrix = [[1,4,7,11,15],[2,5,8,12,19],[3,6,9,16,22],[10,13,14,17,24],[18,21,23,26,30]], target = 5
输出:true

示例 2:

image-20220416231718331

输入:matrix = [[1,4,7,11,15],[2,5,8,12,19],[3,6,9,16,22],[10,13,14,17,24],[18,21,23,26,30]], target = 20
输出:false

提示:

  • m == matrix.length
  • n == matrix[i].length
  • 1 <= n, m <= 300
  • -10^9 <= matrix[i][j] <= 10^9
  • 每行的所有元素从左到右升序排列
  • 每列的所有元素从上到下升序排列
  • -10^9 <= target <= 10^9

解题

方法一:暴力遍历

思路

遍历矩阵中每一个元素与目标值进行对比。

代码

class Solution {
    public boolean searchMatrix(int[][] matrix, int target) {
        for (int[] arr : matrix) {
            for (int num : arr) {
                if (target == num) {
                    return true;
                }
            }
        }
        return false;
    }
}

方法二:遍历 二分查找

思路

遍历每一行数组,每一行中的元素使用二分查找。

代码

class Solution {
    public boolean searchMatrix(int[][] matrix, int target) {
        int colLen = matrix[0].length;
        for (int[] arr : matrix) {
            int start = 0, end = colLen - 1;
            while(start <= end) {
                int mid = start + ((end - start) >> 1);
                if (target == arr[mid]) {
                    return true;
                } else if (target < arr[mid]) {
                    end = mid - 1;
                } else {
                    start = mid + 1;
                }
            }
        }
        return false;
    }
}

方法三:Z形查找

思路

例:

黄色:搜索区域 红框:目标元素 绿色:基准元素

  1. target<topRighttarget < topRight 列缩小
    image-20220416232920724
  2. target<topRighttarget < topRight 列缩小
    image-20220416232852706
  3. target<topRighttarget < topRight 列缩小
    image-20220416233014758
  4. target>topRighttarget > topRight 行缩小
    image-20220416233054759
  5. target>topRighttarget > topRight 行缩小
    image-20220416233140499
  6. target=topRighttarget = topRight 找到目标 返回
    image-20220416233227984

代码

递归
class Solution {
    public int rowLen;

    public boolean searchMatrix(int[][] matrix, int target) {
        rowLen = matrix.length;
        return searchMatrix(matrix, target, 0, matrix[0].length - 1);
    }

    public boolean searchMatrix(int[][] matrix, int target, int rowOffset, int colBound) {
        if (rowOffset > rowLen - 1 || colBound < 0) {
            return false;
        }

        int topRight = matrix[rowOffset][colBound];
        if (target == topRight) {
            return true;
        } else if (target < topRight) {
            return searchMatrix(matrix, target, rowOffset, colBound - 1);
        } else {
            return searchMatrix(matrix, target, rowOffset + 1, colBound);
        }
    }
}
迭代
class Solution {
    public boolean searchMatrix(int[][] matrix, int target) {
        int rowOffset = 0, colBound = matrix[0].length - 1;
        while (rowOffset < matrix.length && colBound >= 0) {
            int topRight = matrix[rowOffset][colBound];
            if (target == topRight) {
                return true;
            } else if (target < topRight) {
                colBound--;
            } else {
                rowOffset++;
            }
        }
        return false;
    }
}
0

评论区