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
题目如下:这两个题目可以用同样的代码来解答,因此就合并在一起了. 题目一: 题目二: 解题思路:两个题目的唯一区别在于第二个题目下一行的最小值不一定会小于前一行的最大值.但是不管怎么样我们可以确定的是 ...
随机推荐
- Nexus网页直接上传jar包
登陆已经安装好的nexus私有仓库,如图: 点击左边菜单“Repositories”,选择右边列表“3rd party“ 点击“3rd party”,选择artifact Upload,如下图 ...
- -bash: locate: command not found
部分版本的linux系统使用locate快速查找某文件路径会报以下错误: -bash: locate: command not found 其原因是没有安装mlocate这个包 安装:yum -y ...
- Packetbeat协议扩展开发教程(1)
Packetbeat ( https://www.elastic.co/products/beats/packetbeat )是一个开源的网络抓包与分析框架,内置了很多常见的协议解析,如HTPP.My ...
- Android ScrollView嵌套ScrollView滚动的问题解决办法
引用:http://mengsina.iteye.com/blog/1707464 http://fenglog.com/article.asp?id=449 Android ScrollView嵌套 ...
- oracle fm格式化
select to_char(0.56,'FM999,999,990.00' ) from dual 其中 9代表如果存在数字则显示数字,不存在显示空格 其中 0代表如果存在数字则显示数字,不存在则显 ...
- saltstack-----上线环境篇(一)
在11上安装mysql yum install mariadb mariadb-server -y 在mysql的配置文件my.cnf中加入 init_connect='SET collation_c ...
- 在python中读写matlab文件
scipy.io提供有两个函数loadmat和savemat,用来读取和存储mat的数据文件 import scipy.io as sio 还有一些其他常用的模块 import numpy as np ...
- IRC and security tools
login::: /msg NickServ identify <password>. join::: /join #metasploit 浏览器: Tor操作系统: Tails加 ...
- 新建虚拟机_XP系统(一)
准备工作:(1)安装VMware (2)下载系统镜像文件 1.新建虚拟机 2.下一步,选择“稍后安装操作系统” 3.选择操作系统类型和版本 4.设置虚拟机名称和安装位置 (安装路径空间要足够大) 5. ...
- 启动HDFS
$cd /app/hadoop/hadoop-2.2.0/sbin $./start-dfs.sh