Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties:

  • Integers in each row are sorted from left to right.
  • The first integer of each row is greater than the last integer of the previous row.

For example,

Consider the following matrix:

[
[1, 3, 5, 7],
[10, 11, 16, 20],
[23, 30, 34, 50]
]

Given target = 3, return true.

二分法的简单变形,把整个数组当成一个长的大数组就可以了,代码如下:

 class Solution {
public:
bool searchMatrix(vector<vector<int>>& matrix, int target) {
int szHor = matrix.size();
if(!szHor) return false;
int szVer = matrix[].size();
if(!szVer) return false;
int totalSize = szHor * szVer;
return bs(matrix, , totalSize - , target, szVer); } bool bs(vector<vector<int>> & matrix, int beg, int end, int target, int colCount)
{
if(beg > end)
return false;
int mid = beg + (end - beg)/;
int val = matrix[mid/colCount][mid%colCount];
if(val == target) return true;
else if(val < target)
return bs(matrix, mid + , end, target, colCount);
else
return bs(matrix, beg, mid - , target, colCount);
} };

LeetCode OJ:Search a 2D Matrix(二维数组查找)的更多相关文章

  1. [算法][LeetCode]Search a 2D Matrix——二维数组的二分查找

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

  2. leetcode——Search a 2D Matrix 二维有序数组查找(AC)

    Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...

  3. [LeetCode] 240. Search a 2D Matrix II 搜索一个二维矩阵 II

    Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...

  4. 【LeetCode】剑指 Offer 04. 二维数组中的查找

    二维数组查找:线性查找法 有二维数组: [  [1,   4,  7, 11, 15],  [2,   5,  8, 12, 19],  [3,   6,  9, 16, 22],  [10, 13, ...

  5. php利用array_search与array_column实现二维数组查找

    利用array_search与array_column实现二维数组查找,不用自己写个循环,减少工作量. <?php $userdb = array( 0 => array( 'uid' = ...

  6. (2)剑指Offer之二维数组查找和替换空格问题

    一 二维数组查找 题目描述: 在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序.请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数. 问 ...

  7. [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 follo ...

  8. [LeetCode] 74 Search a 2D Matrix(二分查找)

    二分查找 1.二分查找的时间复杂度分析: 二分查找每次排除掉一半不合适的值,所以对于n个元素的情况来说: 一次二分剩下:n/2 两次:n/4 m次:n/(2^m) 最坏情况是排除到最后一个值之后得到结 ...

  9. [LeetCode] Range Sum Query 2D - Immutable 二维区域和检索 - 不可变

    Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper lef ...

  10. 【leetcode】Search a 2D Matrix

    Search a 2D Matrix Write an efficient algorithm that searches for a value in an m x n matrix. This m ...

随机推荐

  1. mysql聚合函数操作

    1.mysql对中文进行排序 注:是用convert函数用gb2312编码转换 SELECT * FROM 表名 ORDER BY CONVERT(字段名 USING gb2312 ) ASC;

  2. CSS 之怀疑自己的审美 2 (Day50)

    阅读目录 伪类 选择器的优先级 css 属性操作 一.伪类 anchor伪类:专用于控制链接的显示效果 ''' a:link(没有接触过的链接),用于定义了链接的常规状态. a:hover(鼠标放在链 ...

  3. Django基础一

    Django基础一 Web框架本质 框架,即framework,特指为解决一个开放性问题而设计的具有一定约束性的支撑结构,使用框架可以帮你快速开发特定的系统,简单地说,就是你用别人搭建好的舞台来做表演 ...

  4. day14生成器

    生成器 我自己想写个可迭代的,——生成器生成器的本质就是迭代器因此生成器的所有好处都和迭代器一样但是生成器是我们自己写的python代码生成器的实现有两种方式:1.生成器函数2.生成器表达式 def ...

  5. SQLAlchemy_定义(一对一/一对多/多对多)关系

    目录 Basic Relationship Patterns One To Many One To One Many To Many Basic Relationship Patterns 基本关系模 ...

  6. PHP实现文件下载断点续传

    <?php /* * PHP下载断点续传 * from:php100 */ function dl_file_resume($file){ //检测文件是否存在 if (!is_file($fi ...

  7. 杭电1024Max Sum Plus Plus

    地址:http://acm.hdu.edu.cn/showproblem.php?pid=1024 题目: Problem Description Now I think you have got a ...

  8. linux centos7 安装zookeeper

    linux 系统下 zookeeper 安装教程 1.下载安装包 1)进入安装目录 cd /home/install/ 2)下载 wget http://mirror.bit.edu.cn/apach ...

  9. [Cannot deserialize JSON array into type] NewtonSoft.Json解析数据出错原因

    今天用NewtonSoft.JSon解析一个天气数据,数据格式如: {"status":1,"detail":"\u6570\u636e\u83b7\ ...

  10. [转]React Native 语言基础之ES6

    React Native 是基于 React 这个前端框架来构建native app的架构.React Native基于ES6(即ECMAScript2015)语言进行开发的. JS的组成 1) 核心 ...