美文网首页
面试题04. 二维数组中的查找

面试题04. 二维数组中的查找

作者: 周英杰Anita | 来源:发表于2020-02-27 12:09 被阅读0次

题目描述:

请实现一个函数,把字符串 s 中的每个空格替换成"%20"。
在一个 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

思路1:

1、二分法先找到右边界;
2、二分法再找到左边界;
3、最终返回结果ans;

Java解法:

class Solution {
    public boolean findNumberIn2DArray(int[][] matrix, int target) {
        if(matrix == null || matrix.length == 0 || matrix[0].length == 0)
        {
            return false;
        }
        int rows = matrix.length;
        int colums = matrix[0].length;
        int row = 0;
        int colum = colums - 1;
        while(row < rows && colum >=0)
        {
            if(matrix[row][colum] > target)
            {
                colum --;
            }else if(matrix[row][colum] < target){
                row ++;
            }else{
                return true;
            }
        }
        return false;
    }
}

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/er-wei-shu-zu-zhong-de-cha-zhao-lcof

相关文章

  • LeetCode | 面试题04. 二维数组中的查找【剑指Off

    LeetCode 面试题04. 二维数组中的查找【剑指Offer】【Easy】【Python】【数组】 问题 力扣...

  • 剑指offer

    面试题3——数组中重复的数字 使用LinkedHashMap,有序存放。 面试题4——二维数组中的查找 首先选...

  • 刷题1 剑指 Offer — 数组和字符串

    剑指 Offer 04. 二维数组中的查找 https://leetcode-cn.com/leetbook/re...

  • 面试题4

    剑指 Offer 04. 二维数组中的查找[https://leetcode-cn.com/problems/er...

  • 剑指 Offer 04 二维数组中的查找

    剑指 Offer 04. 二维数组中的查找[https://leetcode-cn.com/problems/er...

  • 2022.4.28

    剑指 Offer 04. 二维数组中的查找[https://leetcode.cn/problems/er-wei...

  • 剑指Offer(一)

    题目汇总03.数组中重复的数字(简单),本题考查数组04.二维数组中的查找(简单),本题考查数组05.替换空格,本...

  • 剑指offer面试题分类总结

    数组: 面试题3:数组中重复的数字面试题4:二维数组中的查找面试题21:调整数组顺序使奇数位于偶数前面面试题39:...

  • 2.3.1 数组

    面试题3:数组中重复的数字 面试题4:二维数组中的查找 在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一...

  • 70.二维数组中的查找

    day17: 剑指 Offer 04. 二维数组中的查找[https://leetcode-cn.com/p...

网友评论

      本文标题:面试题04. 二维数组中的查找

      本文链接:https://www.haomeiwen.com/subject/weifchtx.html