[菜鸟训练]剑指 Offer 04. 二维数组中的查找(中等题目 思维题)

题目描述:

在一个 n * m 的二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个高效的函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。

示例:

现有矩阵 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。
给定 target = 20,返回 false。

限制:

0 <= n <= 1000

0 <= m <= 1000

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/er-wei-shu-zu-zhong-de-cha-zhao-lcof
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

解题思路:

该题目是一个思维题,也可以将其当作找规律。因为该数组在每一行和每一列上都有序,所以可以选取第一列或者第一行最大的元素来进行判断,接下来以选取第一行最大元素来进行说明。
以上述题目所给的样例来进行说明,target=5;首先选择最右上角的15来与target进行判断,明显15要大于5,所以这里说明,15所在的列都比5大,接下来可以不予考虑,于是我们就把该55的二维数组删减成了54的二维数组。

重复上述操作。将11与target相比,依旧是11大,所以11所在列都比5大,将11所在列删去,数组删减成了53的二维数组。将7和target相比,同上,数组删减成52的二维数组。接着,4和target相比,4比5小,所以4所在行不予考虑(此时别忘了我门的数组已经删减为52的二维数组),所以我们将数组再次删减为42的二维数组。再次执行此操作,我们找的了数组中所存在的5,所以返回true。

**注意:**这里的将数组进行删减并不是真的进行删减,而是删减部分不予考虑,即里面肯定不存在与target相同的元素,所以没必要浪费时间去比较。

图解:

在这里插入图片描述

package com.lzw.test1;

/**
 * 在一个 n * m 的二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。
 * 请完成一个高效的函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。
 *
 * 示例:
 *
 * 现有矩阵 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。
 *
 * 给定target=20,返回false。

 *
 * 限制:
 *
 * 0 <= n <= 1000
 *
 * 0 <= m <= 1000
 *
 * @author puppet
 * @create 2021-03-19-9:19
 */
public class jianzhi_Offer_04 {
    
    
    //从右上角开始
    public boolean findNumberIn2DArray(int[][] matrix, int target) {
    
    
        boolean flag = false;
        if (matrix != null && matrix.length > 0 && matrix[0].length > 0){
    
    
            int row = matrix.length - 1;
            int col = matrix[0].length - 1;
            int n = row;
            int m = col;

            while (row >=0 && col >=0){
    
    
                if(matrix[n - row][col] == target){
    
    
                    flag = true;
                    break;
                }
                else if(matrix[n - row][col] < target){
    
    
                    row--;
                }
                else {
    
    
                    col--;
                }
            }
        }
        return flag;
    }

    //从左下角开始
    public boolean findNumberIn2DArray1(int[][] matrix, int target) {
    
    
        boolean flag = false;
        if (matrix != null && matrix.length > 0 && matrix[0].length > 0){
    
    
            int row = matrix.length - 1;
            int col = matrix[0].length - 1;
            int n = row;
            int m = col;

            while (row >=0 && col >= 0){
    
    
                if(matrix[row][m - col] == target){
    
    
                    flag = true;
                    break;
                }
                else if(matrix[row][m - col] < target){
    
    
                    col--;
                }
                else {
    
    
                    row--;
                }
            }
        }
        return flag;
    }

    public static void main(String[] args) {
    
    
        int[][] matrix = new int[][]{
    
    {
    
    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}};
        jianzhi_Offer_04 obj = new jianzhi_Offer_04();
        System.out.println(obj.findNumberIn2DArray1(matrix, 5));
    }
}

猜你喜欢

转载自blog.csdn.net/Puppet__/article/details/115000588