74. Search a 2D Matrix(二分查找,剑指offer 1)
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)的更多相关文章
- 二维数组中的查找 - Java版 -简单二分查找 -<<剑指Offer>> -水题
如题 (总结) -认真读题, 还WA了一次, https://www.nowcoder.com/practice/abc3fe2ce8e146608e868a70efebf62e?tpId=13&am ...
- [LeetCode] 74 Search a 2D Matrix(二分查找)
二分查找 1.二分查找的时间复杂度分析: 二分查找每次排除掉一半不合适的值,所以对于n个元素的情况来说: 一次二分剩下:n/2 两次:n/4 m次:n/(2^m) 最坏情况是排除到最后一个值之后得到结 ...
- leetcode 74. Search a 2D Matrix 、240. Search a 2D Matrix II
74. Search a 2D Matrix 整个二维数组是有序排列的,可以把这个想象成一个有序的一维数组,然后用二分找中间值就好了. 这个时候需要将全部的长度转换为相应的坐标,/col获得x坐标,% ...
- [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 ...
- 【LeetCode】74. Search a 2D Matrix 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 左下或者右上开始查找 顺序查找 库函数 日期 题目地 ...
- 【一天一道LeetCode】#74. Search a 2D Matrix
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Write a ...
- 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 ...
- 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 ...
- 【leetcode】74. Search a 2D Matrix & 240. Search a 2D Matrix II
题目如下:这两个题目可以用同样的代码来解答,因此就合并在一起了. 题目一: 题目二: 解题思路:两个题目的唯一区别在于第二个题目下一行的最小值不一定会小于前一行的最大值.但是不管怎么样我们可以确定的是 ...
随机推荐
- X-Requested-With导致CSRF失败
在漫漫渗透之路中,眼前一亮的发现一个站.Referer字段没有检查,POST参数中的动态token也没有检查,这不是带一波CSRF的节奏嘛.但是遇到一个之前我没遇到的问题导致我CSRF失败,这个问题或 ...
- 【转】Navigation Drawer(导航抽屉)
创建一个导航抽屉 创建抽屉布局 初始化抽屉列表 处理导航项选点击事件 监听导航抽屉打开和关闭事件 点击应用图标来打开和关闭导航抽屉 创建一个导航抽屉 导航抽屉是一个位于屏幕左侧边缘用来显示应用程序 ...
- 几个解决k染色问题的指数级做法
几个解决k染色问题的指数级做法 ——以及CF908H题解 给你一张n个点的普通无向图,让你给每个点染上k种颜色中的一种,要求对于每条边,两个端点的颜色不能相同,问你是否存在一种可行方案,或是让你输出一 ...
- 常见的几个js疑难点,match,charAt,charCodeAt,map,search
JavaScript match() 方法 定义和用法 match() 方法可在字符串内检索指定的值,或找到一个或多个正则表达式的匹配. 该方法类似 indexOf() 和 lastI ...
- CentOS 6.8下更改Apache默认网站安装目录
首先,当我们搭建好LAMP环境后,Apache服务器默认的网站安装目录是/var/www/html 然而我们搭建项目时为了方便,我们是按照自己的意愿更改网站目录的 现在假设,按照意愿设定的网站目录为/ ...
- Pyqt图标下载网站
下载地址: https://www.easyicon.net/ 1.程序中图标建议使用32x32的PNG格式. 2.pyinstaller打包中图标建议使用32x32的ICO格式.
- pandas category数据类型
实际应用pandas过程中,经常会用到category数据类型,通常以string的形式显示,包括颜色(红,绿,蓝),尺寸的大小(大,中,小),还有地理信息等(国家,省份),这些数据的处理经常会有各种 ...
- gevent 真正的协程
import gevent #第一次使用需要cmd窗口敲入 pip install Gevent from gevent import monkey:monkey.patch_all import t ...
- Ubuntu:14.04.2 安装多个Linux内核
http://blog.csdn.net/ddk3001/article/details/47340119 安装Ubuntu 14.04.2 后,内核是 3.16.0-30-generic 1.虚 ...
- Catch---hdu3478(染色法判断是否含有奇环)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3478 题意:有n个路口,m条街,一小偷某一时刻从路口 s 开始逃跑,下一时刻都跑沿着街跑到另一路口,问 ...