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.

Example 1:

Input:
matrix = [
[1, 3, 5, 7],
[10, 11, 16, 20],
[23, 30, 34, 50]
]
target = 3
Output: true

Example 2:

Input:
matrix = [
[1, 3, 5, 7],
[10, 11, 16, 20],
[23, 30, 34, 50]
]
target = 13
Output: false
class Solution {
public:
bool searchMatrix(vector<vector<int>>& a, int target) {
int m = a.size();
if (m==0) return false;
int n = a[0].size();
if (n==0) return false;
//get target in which col
int lo = 0;
int hi = m;
while(lo<=hi) {
int mid = lo + (hi - lo) / 2;
if (mid>=m) break;
if (target < a[mid][0]) {
hi = mid - 1;
} else if (a[mid][0] < target) {
lo = mid + 1;
} else {
return true;
}
}
std::cout << lo <<std::endl;
int col = lo-1;
if (col<0 ||col>m) return false;
//get target in col
lo = 0;
hi = n;
while(lo<=hi) {
int mid = lo + (hi - lo) / 2;
if (mid>=n) break;
if (target < a[col][mid]) {
hi = mid - 1;
} else if (a[col][mid] < target) {
lo = mid + 1;
} else {
return true;
}
}
return false;
}
};

  


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

74. Search a 2D Matrix(二分查找,剑指offer 1)的更多相关文章

  1. 二维数组中的查找 - Java版 -简单二分查找 -<<剑指Offer>> -水题

    如题 (总结) -认真读题, 还WA了一次, https://www.nowcoder.com/practice/abc3fe2ce8e146608e868a70efebf62e?tpId=13&am ...

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

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

  3. leetcode 74. Search a 2D Matrix 、240. Search a 2D Matrix II

    74. Search a 2D Matrix 整个二维数组是有序排列的,可以把这个想象成一个有序的一维数组,然后用二分找中间值就好了. 这个时候需要将全部的长度转换为相应的坐标,/col获得x坐标,% ...

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

  5. 【LeetCode】74. Search a 2D Matrix 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 左下或者右上开始查找 顺序查找 库函数 日期 题目地 ...

  6. 【一天一道LeetCode】#74. Search a 2D Matrix

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Write a ...

  7. 74. Search a 2D Matrix

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

  8. LeetCode OJ 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 ...

  9. 【leetcode】74. Search a 2D Matrix & 240. Search a 2D Matrix II

    题目如下:这两个题目可以用同样的代码来解答,因此就合并在一起了. 题目一: 题目二: 解题思路:两个题目的唯一区别在于第二个题目下一行的最小值不一定会小于前一行的最大值.但是不管怎么样我们可以确定的是 ...

随机推荐

  1. ThinkPHP-5.0.23新的RCE漏洞测试和POC

    TP5新RCE漏洞 昨天又是周五,讨厌周五曝漏洞,还得又得加班,算了,还是先验证一波.新的TP5RCE,据说发现者因为上次的RCE,于是又审计了代码,结果发现的.TP5也成了万人轮啊. 测试 环境搭建 ...

  2. 3-1 vue-resource基础介绍

    1.静态引用 <script src="https://cdn.bootcss.com/vue-resource/1.3.4/vue-resource.js">< ...

  3. ubuntu 14.04 升级到 16.04 问题总结

    1. 需要的依赖关系未安装 The required dependency 'apt (>= 1.0.1ubuntu2.13)' is not installed. http://forum.u ...

  4. 安装 SQL SERVER MsiGetProductInfo 无法检索 Product Code 1605错误 解决方案

    重装数据库服务器上的SQL SERVER 2008 上遇到了以下问题 标题: SQL Server 安装程序失败. SQL Server 安装程序遇到以下错误: MsiGetProductInfo 无 ...

  5. ElasticSearch概述及Linux下的单机ElasticSearch安装

    原文链接:http://blog.csdn.net/w12345_ww/article/details/52182264 这两天在项目中要涉及到ElasticSearch的使用,就上网去搜索了一些这方 ...

  6. SOA架构商城二 框架搭建

    1.创建父工程 创建Maven工程pingyougou-parent,选择packaging类型为pom ,在pom.xml文件中添加锁定版本信息dependencyManagement与plugin ...

  7. ggplot2绘制概率密度图

    以下绘图以Weibull分布(韦伯分布.威布尔分布)为例 关于Weibull分布(韦伯分布.威布尔分布),请参考本人博客http://www.cnblogs.com/wwxbi/p/6141501.h ...

  8. ELK之filebeat-redis-logstash-es构架模式

    下载filebeat的rpm包安装filebeat wget https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-6.3.0- ...

  9. Python实现KNN算法及手写程序识别

    1.Python实现KNN算法 输入:inX:与现有数据集(1xN)进行比较的向量   dataSet:已知向量的大小m数据集(NxM)   个标签:数据集标签(1xM矢量)   k:用于比较的邻居数 ...

  10. Oracle管理监控之sqlplus实现上下翻页设置

    环境: 操作系统:red hat 5.8 OS 数据库:oracle 11g 11.2.0.1 安装软件:IO-Tty-1.07.tar. Term-ReadLine-Gnu-1.16.tar.uni ...