二分查找要注意边界值的取值,边界情况的判定

题目描述

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

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

示例 1:

输入:matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 3

输出:true

示例 2:

输入:matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 13

输出:false

提示:

  • m == matrix.length
  • n == matrix[i].length
  • 1 <= m, n <= 100
  • -104 <= matrix[i][j], target <= 104

解答

解法一 先搜索在哪一行再搜索某一行

算法复杂度\(O(m+n)\)

class Solution {
public boolean searchMatrix(int[][] matrix, int target) {
int len = matrix.length;
int n = matrix[0].length;
for (int i = 0; i < len ; ++i) {
if (target >= matrix[i][0] && i + 1 <= len - 1 && target < matrix[i+1][0]) {
for (int j = 0; j < n; ++j) {
if (matrix[i][j] == target) {
return true;
}
}
}
else if (target >= matrix[i][0] && i == len - 1) {
for (int j = 0; j < n; ++j) {
if (matrix[i][j] == target) {
return true;
}
}
}
}
return false;
}
}

解法二 在解法一的基础上二分查找

算法复杂度\(O(log(mn))\)

class Solution {
public boolean searchMatrix(int[][] matrix, int target) {
int m = matrix.length;
int n = matrix[0].length;
int m1 = findm(matrix, target, 0, m-1);
if (m1==-1) {return false;}
return findn(matrix[m1], target, 0, n-1);
}
public int findm(int[][] matrix, int target, int s, int t) { if (s == t) {
return target >= matrix[s][0] && target <= matrix[s][matrix[0].length-1] ? s : -1;
}
int mid = (s + t) >> 1;
if (target >= matrix[mid][0] && target < matrix[mid+1][0]) {
return mid;
}
else if (target > matrix[mid][0]) {
// 这里选择 mid+1 是为什么,细品一下
return findm(matrix, target, mid + 1, t);
}
else {
// 这里选择 mid 为什么不是 mid-1,继续品
return findm(matrix, target, s, mid);
}
}
public boolean findn(int[] matrix, int target, int s, int t) { if (s == t) {
return matrix[s] == target || matrix[t] == target;
}
int mid = (s + t) >> 1;
if (target == matrix[mid]) {
return true;
}
else if (matrix[mid] < target) {
return findn(matrix, target, mid + 1, t);
}
else {
return findn(matrix, target, s, mid);
}
}
}

解法三 将二维数组当做一维数组,二分查找

算法复杂度为\(O(log(m+n))\)

class Solution {
public boolean searchMatrix(int[][] matrix, int target) {
int m = matrix.length;
int n = matrix[0].length; int pi = 0, pj = m * n - 1; while (pj > pi) {
int mid = (pi + pj) >> 1;
int i = mid / n;
int j = mid % n; if (matrix[i][j] == target) {
return true;
}
else if (matrix[i][j] > target) {
pj = mid;
continue;
}
else {
pi = mid + 1;
continue;
}
}
if (pi == pj) {
int i = pi / n;
int j = pi % n;
return matrix[i][j] == target;
}
return false;
}
}

leetcode 刷题(数组篇)74 题 搜索二维矩阵 (二分查找)的更多相关文章

  1. LeetCode:搜索二维矩阵【74】

    LeetCode:搜索二维矩阵[74] 题目描述 编写一个高效的算法来判断 m x n 矩阵中,是否存在一个目标值.该矩阵具有如下特性: 每行中的整数从左到右按升序排列. 每行的第一个整数大于前一行的 ...

  2. LeetCode 74. 搜索二维矩阵(Search a 2D Matrix)

    74. 搜索二维矩阵 74. Search a 2D Matrix 题目描述 编写一个高效的算法来判断 m x n 矩阵中,是否存在一个目标值.该矩阵具有如下特性: 每行中的整数从左到右按升序排列. ...

  3. 【leetcode】74. 搜索二维矩阵

    题目链接:传送门 题目描述 编写一个高效的算法来判断 m x n 矩阵中,是否存在一个目标值.该矩阵具有如下特性: 每行中的整数从左到右按升序排列. 每行的第一个整数大于前一行的最后一个整数. 示例  ...

  4. Java实现 LeetCode 74 搜索二维矩阵

    74. 搜索二维矩阵 编写一个高效的算法来判断 m x n 矩阵中,是否存在一个目标值.该矩阵具有如下特性: 每行中的整数从左到右按升序排列. 每行的第一个整数大于前一行的最后一个整数. 示例 1: ...

  5. Leetcode之二分法专题-240. 搜索二维矩阵 II(Search a 2D Matrix II)

    Leetcode之二分法专题-240. 搜索二维矩阵 II(Search a 2D Matrix II) 编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target.该矩阵 ...

  6. LeetCode 240. 搜索二维矩阵 II(Search a 2D Matrix II) 37

    240. 搜索二维矩阵 II 240. Search a 2D Matrix II 题目描述 编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target.该矩阵具有以下特性 ...

  7. Leetcode 240.搜索二维矩阵II

    搜索二维矩阵II 编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target.该矩阵具有以下特性: 每行的元素从左到右升序排列. 每列的元素从上到下升序排列. 示例: 现有 ...

  8. Java实现 LeetCode 240 搜索二维矩阵 II(二)

    240. 搜索二维矩阵 II 编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target.该矩阵具有以下特性: 每行的元素从左到右升序排列. 每列的元素从上到下升序排列. ...

  9. LeetCode74.搜索二维矩阵

    74.搜索二维矩阵 描述 编写一个高效的算法来判断 m x n 矩阵中,是否存在一个目标值.该矩阵具有如下特性: 每行中的整数从左到右按升序排列. 每行的第一个整数大于前一行的最后一个整数. 示例 示 ...

  10. lintcode:搜索二维矩阵II

    题目 搜索二维矩阵 II 写出一个高效的算法来搜索m×n矩阵中的值,返回这个值出现的次数. 这个矩阵具有以下特性: 每行中的整数从左到右是排序的. 每一列的整数从上到下是排序的. 在每一行或每一列中没 ...

随机推荐

  1. 浅析VAST代币与SPC算力币的释放模式

    许多区块链业界分析师认为,2021年的区块链市场或许与2020年的有些许不同.2020年的区块链市场更注重的是DeFi领域,很多公链项目以及资本市场的巨鲸们也是将目光锁定在DeFi领域.而2021年的 ...

  2. IDEA 敏捷开发技巧——后缀完成

    前言 "工欲善其事,必先利其器." 所以说今天来看一看如何压榨 IDEA ,让你的 IDEA 使用的更顺手! 今日技巧: 后缀完成 自定义后缀完成模版 示例 上面动图使用了 .so ...

  3. JDK源码阅读-RandomAccessFile

    本文转载自JDK源码阅读-RandomAccessFile 导语 FileInputStream只能用于读取文件,FileOutputStream只能用于写入文件,而对于同时读取文件,并且需要随意移动 ...

  4. 1004 Counting Leaves ——PAT甲级真题

    1004 Counting Leaves A family hierarchy is usually presented by a pedigree tree. Your job is to coun ...

  5. How DRI and DRM Work

    How DRI and DRM Work Introduction This page is intended as an introduction to what DRI and DRM are, ...

  6. vue3与vue2的区别

    全局属性 vue2 对于一些第三方插件,vue2中通常使用prototype原型来挂载到vue对象中 import Vue from 'vue' Vue.prototype.$http=Axiox V ...

  7. 使用 Java 开发 Gradle 插件

    Gradle 插件代码可以在 build.gradle 中,buildSrc 项目中,以及独立的插件项目中编写.本文将介绍如何在一个独立的项目中使用 Java 语言编写 Gradle 插件,并发布到仓 ...

  8. 盘点Excel中的那些有趣的“bug”

    本文由葡萄城技术团队原创并首发 转载请注明出处:葡萄城官网,葡萄城为开发者提供专业的开发工具.解决方案和服务,赋能开发者. Excel 1.0早在1985年正式进入市场,距今已经有36年了,虽然在推出 ...

  9. 翻译:《实用的Python编程》03_06_Design_discussion

    目录 | 上一节 (3.5 主模块) | 下一节 (4 类) 3.6 设计讨论 本节,我们重新考虑之前所做的设计决策. 文件名与可迭代对象 考虑以下两个返回相同输出的程序. # Provide a f ...

  10. 用vue.js实现的期货,股票的实时K线

    用vue.js实现的期货,股票的实时k线 项目地址:https://github.com/zhengquantao/vue-Kline vue-kline 效果图 Build Setup 本项目基于V ...