最优的做法就是从右上角开始。
如果当前点大于target, colIndex–;
如果当前点小于target, rowIndex++;
否则 colIndex–; rowIndex++; count++;

该方法也可以从左下角开始。

Time complexity O(m+n).
Space complexity O(1).

注意:这题的条件没有LintCode 28: Search a 2D Matrix那么严格,那道题是每行元素开头都大于上一行元素结束。所以LintCode 28的binary search解法不能用于这题,但这题的解法可以用于LintCode 28.
代码如下:

class Solution {
public:/*** @param matrix: A list of lists of integers* @param target: An integer you want to search in matrix* @return: An integer indicate the total occurrence of target in the given matrix*/int searchMatrix(vector<vector<int>> &matrix, int target) {int nRow = matrix.size();if (nRow == 0) return 0;int nCol = matrix[0].size();int count = 0;int rowIndex = 0;int colIndex = nCol - 1;  //start from the right top pointwhile (rowIndex < nRow && colIndex >= 0) {if (matrix[rowIndex][colIndex] > target) {colIndex--;} else if (matrix[rowIndex][colIndex] < target) {rowIndex++;} else {count++;colIndex--;rowIndex++;}}return count;    }
};

LintCode 38: Search a 2D Matrix II相关推荐

  1. LeetCode 240. Search a 2D Matrix II

    LeetCode 240. Search a 2D Matrix II Solution1: 为什么把第74题的代码改都不用改的拿过来就可以AC,一脸懵逼啊... class Solution { p ...

  2. LeetCode Search a 2D Matrix II

    问题:给出一个二维数组,其行,列是递增的,和一个要查找的数,问矩阵中是否有要查找的数 思路: 第一种方法是直接暴力查找,在二维数组中查找. 第二种方法是基于对角线上,在行,列上作二分查找 第三种方法递 ...

  3. leetcode 240. Search a 2D Matrix II | 240. 搜索二维矩阵 II(Java)

    题目 https://leetcode.com/problems/search-a-2d-matrix-ii/ 题解 方法1 思路类似于 leetcode 200. Number of Islands ...

  4. LeetCode-240 Search a 2D Matrix II

    题目描述 Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the ...

  5. [Leetcode] 240. Search a 2D Matrix II 解题报告

    题目: Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the f ...

  6. Leetcode 240 Search a 2D Matrix II (二分法和分治法解决有序二维数组查找)

    1.问题描述 写一个高效的算法,从一个 m×n m\times n的整数矩阵中查找出给定的值,矩阵具有如下特点: 每一行从左到右递增. 每一列从上到下递增. 2. 方法与思路 2.1 二分查找法 根据 ...

  7. 【LeetCode 剑指offer刷题】矩阵题1:4 有序矩阵中的查找( 74. Search a 2D Matrix )(系列)...

    [LeetCode & 剑指offer 刷题笔记]目录(持续更新中...) 74. Search a 2D Matrix Write an efficient algorithm that s ...

  8. LeetCode 74. Search a 2D Matrix

    LeetCode 74. Search a 2D Matrix Solution1:我的答案 <剑指offer>原题 class Solution { public:bool search ...

  9. LintCode: Search A 2d Matrix

    1. 设查找的数位y,第一行最后一列的数位x 如果x<y,x是第一行最大的,所以第一行都小于y,删除第一行: 如果x>y,x是最后一列最小的,所以最后一列都大于y,删除最后一列: 这样保证 ...

  10. leetcode 74 java_【LeetCode】74. Search a 2D Matrix

    问题描述 Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the ...

最新文章

  1. java包名命名规范[【转】
  2. 深入理解Java虚拟机-Java内存区域透彻分析
  3. 基于 DataLakeAnalytics 做跨地域的数据分析
  4. AutoHotKey程序防止反编译的简单手段
  5. Vue项目中使用 路由导航守卫 处理页面的访问权限
  6. 史上最全的前端开发面试题(含详细答案)
  7. JavaScript——面向对象之继承(原型对象)与多态(重载、重写)
  8. 数据结构与算法系列——排序(10)_归并排序
  9. php相差天数,php获取给定日期相差天数
  10. Xshell6 Xftp6 破解
  11. Abaqus齿轮有限元分析
  12. 【已解决】STM32芯片下载 Flash Download failed -“Cortex-M3”问题
  13. APP抓包工具Fidder
  14. GCD中dispatch_group的使用方法
  15. MD-Pandoc进阶教程:图表、文献和目录
  16. 计算机基础知识 pdf答案,计算机基础知识试题及答案(一).pdf
  17. cv2不能识别gif
  18. Altium Designer(七)已有原理图生成原理图库
  19. 记录下Tesla V100s vmware EXTI 7.0 虚拟机直通显卡cuda、cudnn安装
  20. AirSim学习和踩坑记录(不定时更新)

热门文章

  1. 20212022最新Web前端经典面试试题及答案-史上最全前端面试题(含答案)、前端面试题大全、前端进阶必知必会知识点1
  2. c语言作业的搜题软件下载,C语言题库app下载-C语言题库下载v2.3 安卓版-西西软件下载...
  3. android课程设计闹钟,EDA课程设计---数字时钟(闹钟)
  4. 计算机开机界面图片怎么修改,电脑win7系统怎么修改开机画面的方法
  5. java jxl.jar_jxl.jar官方下载-jxl.jar(java操作excel) 免费版 - 河东下载站
  6. c语言开发蓝牙驱动 win7,ATK-hc05蓝牙串口模块驱动
  7. 探索FireMonkey的StyleBook皮肤控件的使用
  8. 医药行业数据分析软件(含非付费)--对比分析
  9. 换信科技B2B供应链平台“换易宝”1.0产品上线发布会盛大召开
  10. 低级鼠标钩子WH MOUSE LL