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. linux c编程:互斥锁条件变量

    条件变量:等待与信号发送 使用互斥锁虽然可以解决一些资源竞争的问题,但互斥锁只有两种状态(加锁和解锁),这限制了互斥锁的用途. 条件变量(条件锁)也可以解决线程同步和共享资源访问的问题,条件变量是对互 ...

  2. 0408-服务注册与发现-Eureka常用配置

    一.概述 参看地址:https://cloud.spring.io/spring-cloud-static/Edgware.SR3/single/spring-cloud.html#_appendix ...

  3. redis实现cache系统原理(五)

    1. 介绍 cache就是人们所说的缓存.我们这里所说的cache是web上的.对用户来说,衡量一个网站是否具有良好的体验,其中一个标准就是响应速度的快慢.可能网站刚上线,功能还较少,数据库的记录也不 ...

  4. Boost scoped_ptr scoped_array 以及scoped_ptr与std::auto_ptr对比

    boost::scoped_ptr和std::auto_ptr非常类似,是一个简单的智能指针,它能够保证在离开作用域后对象被自动释放.下列代码演示了该指针的基本应用: #include <str ...

  5. s5_day11作业

    # 1 文件内容如下,标题为:姓名,性别,年纪,薪资 # # egon male 18 3000 # alex male 38 30000 # wupeiqi female 28 20000 # yu ...

  6. Java并发(5):同步容器

    一. 同步容器出现的原因 在Java的集合容器框架中,主要有四大类别:List.Set.Queue.Map. List.Set.Queue接口分别继承了Collection接口,Map本身是一个接口. ...

  7. (转)HttpWebRequest以UTF-8编码写入内容时发生“Bytes to be written to the stream exceed the Content-Length bytes size specified.”错误

    from:http://www.cnblogs.com/Gildor/archive/2010/12/13/1904060.html HttpWebRequest以UTF-8编码写入内容时发生“Byt ...

  8. mongodb-2.6.0 在win7 64下的安装和服务启动

    转自: http://blog.csdn.net/lingchen214/article/details/24537629 1   自定义安装到C:\mongodb目录下. 2  手动在C:\mong ...

  9. linux(6/17)--文件打包上传和下载

    tar命令 命令功能 用来压缩和解压文件 命令格式 tar[必要参数][选择参数][文件] tar打包工具 -f ##指定生成包的名字,建议 -f单独写成一个参数 --delete filename ...

  10. 【笔记】c++文件

    1.文件. #include <iostream> #include <cstdio> #include <string> #include <cstring ...