74. Search a 2D Matrix (Graph; Divide-and-Conquer)
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 start = , end = matrix.size()-;
int mid;
int lineNum;
while(start<=end){
mid = start + ((end-start)>>);
if(matrix[mid][]<target){
start = mid+;
}
else if(matrix[mid][]>target){
end = mid-;
}
else return true;
}
if(end < ) return false;
lineNum = end;
start = ;
end = matrix[].size()-;
while(start<=end){
mid = start + ((end-start)>>);
if(matrix[lineNum][mid]<target){
start = mid+;
}
else if(matrix[lineNum][mid]>target){
end = mid-;
}
else return true;
}
return false;
}
};
74. Search a 2D Matrix (Graph; Divide-and-Conquer)的更多相关文章
- [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/ 目录 题目描述 题目大意 解题方法 左下或者右上开始查找 顺序查找 库函数 日期 题目地 ...
- 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] 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
Difficulty:medium More:[目录]LeetCode Java实现 Description Write an efficient algorithm that searches f ...
- 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
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...
随机推荐
- Office 365 开发 集成VS2013 (一)
博客地址 http://blog.csdn.net/foxdave 题外话:好久不写了,个人比较懒,有时候想写东西的时候想一想就又不知从何下笔了.之前因为某些机缘发现自己完全是个管理外行,所以最近下了 ...
- pshell远程连接服务器
在页面添加ip 和 端口 还有 用户,我这里填的是服务器root用户 成功之后 端口后是可以改的 首先看下ssh是否启动 rpm -qa | grep ssh 有的话就是vi /etc/ ...
- keras_基本网络层结构(2)_卷积层
参考文献:http://keras-cn.readthedocs.io/en/latest/layers/convolutional_layer/ 卷积层 Conv1D层 keras.layers.c ...
- VC++ 6.0 C8051F340 MFC programming note
/************************************************************************************** * VC++ 6.0 C ...
- UITableView-(单元格的自定义方法)
//contentView //行内容 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NS ...
- java - 百钱百鸡小算法
传送门: 袁咩咩的小小博客 百钱百鸡是一个非常经典的不定方程问题,最早源于我国古代的<算经>,这是古代著名数学家张丘建首次提出的.百钱百鸡问题原文如下: 鸡翁一,值钱五,鸡母一,值钱三,鸡 ...
- 《DSP using MATLAB》示例 Example 9.11
代码: %% ------------------------------------------------------------------------ %% Output Info about ...
- Oracle-SQL高级查询
--一个题目涉及到的50个Sql语句 --(下面表的结构以给出,自己在数据库中建立表.并且添加相应的数据,数据要全面些. 其中Student表中,SId为学生的ID) ---------------- ...
- 494 - Kindergarten Counting Game
Kindergarten Counting Game Everybody sit down in a circle. Ok. Listen to me carefully. ``Woooooo, ...
- Docker中配置国内镜像
1. 为什么要为docker配置国内镜像 在正常情况下,docker有一个默认连接的国外官方镜像,在国外的网友访问该官方镜像自然不成问题,但是国内毕竟不是国外,由于国情不同,中国的网络访问国外官方 ...