LeetCode OJ-- Search a 2D Matrix
https://oj.leetcode.com/problems/search-a-2d-matrix/
具有数据递增性质的一个二维数组,对它进行二分搜索。
首先搜索target所在的行,再找列。
class Solution {
public:
bool searchMatrix(vector<vector<int> > &matrix, int target) {
if(matrix.size() == )
return false;
size_t col = matrix[].size();
if(col == )
return false; //first find row
int lowRow = , highRow = matrix.size() - ;
int targetRow = ;
while(lowRow <= highRow && highRow >= && lowRow< matrix.size())
{
int midRow = (lowRow + highRow)/;
if(target < matrix[midRow][])
{
highRow = midRow - ;
if(highRow < )
return false;
}
else if(target == matrix[midRow][] || target == matrix[midRow][col - ])
return true;
else if(col!= && target < matrix[midRow][col - ])
{
targetRow = midRow;
break;
}
else
{
lowRow = midRow + ;
if(lowRow >= matrix.size())
return false;
}
} //find col binary search
int lowCol = , highCol = col;
while(lowCol <= highCol)
{
int midCol = (lowCol + highCol)/;
if(matrix[targetRow][midCol] == target)
return true;
else if(matrix[targetRow][midCol] < target)
lowCol = midCol + ;
else
highCol = midCol - ;
}
return false;
}
};
LeetCode OJ-- Search a 2D Matrix的更多相关文章
- [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 ...
- [LeetCode] 74 Search a 2D Matrix(二分查找)
二分查找 1.二分查找的时间复杂度分析: 二分查找每次排除掉一半不合适的值,所以对于n个元素的情况来说: 一次二分剩下:n/2 两次:n/4 m次:n/(2^m) 最坏情况是排除到最后一个值之后得到结 ...
- [Leetcode Week13]Search a 2D Matrix
Search a 2D Matrix 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/search-a-2d-matrix/description/ D ...
- [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】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 ...
- 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 解题思路
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...
- leetcode 【Search a 2D Matrix 】python 实现
题目: 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 ...
随机推荐
- python爬取豆瓣top250的电影数据并存入excle
爬取网址: https://movie.douban.com/top250 一:爬取思路(新手可以看一下) : 1:定义两个函数,一个get_page函数爬取数据,一个save函数保存数据,mian中 ...
- Flask初学者:session操作
cookie:是一种保存数据的格式,也可以看成是保存数据的一个“盒子”,服务器返回cookie给浏览器(由服务器产生),由浏览器保存在本地,下次再访问此服务器时浏览器就会自动将此cookie一起发送给 ...
- python3爬虫之Urllib库(一)
上一篇我简单说了说爬虫的原理,这一篇我们来讲讲python自带的请求库:urllib 在python2里边,用urllib库和urllib2库来实现请求的发送,但是在python3种在也不用那么麻烦了 ...
- 饭卡 HDU - 2546(dp)
电子科大本部食堂的饭卡有一种很诡异的设计,即在购买之前判断余额.如果购买一个商品之前,卡上的剩余金额大于或等于5元,就一定可以购买成功(即使购买后卡上余额为负),否则无法购买(即使金额足够).所以大家 ...
- HBase(0.94.5)的Compact和Split源码分析
经过对比,0.94.5以后版本主要过程基本类似(有些新功能和细节增加) 一. Compact 2.1. Compact主要来源 来自四个方面:1.Memstoreflush时:2.HR ...
- python协程--yield和yield from
字典为动词“to yield”给出了两个释义:产出和让步.对于 Python 生成器中的 yield 来说,这两个含义都成立.yield item 这行代码会产出一个值,提供给 next(...) 的 ...
- leetcode 【 Unique Paths 】python 实现
题目: A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). ...
- 解决pymysql不能实时查询最新的数据
#在网上查询到的原因为: InnoDB 的默认隔离级别.它可以防止任何被查询的行被其他事务更改,从而阻止不可重复的读取,而不是 幻读取.它使用中度严格的锁定策略,以便事务内的所有查询都会查看同一快照中 ...
- Android 数据存储-文件读写操作
本来已经写了一部分,后来发现这篇博客写的比我的好,就直接引用一下: https://www.cnblogs.com/LiHuiGe8/p/5604725.html
- Memcached相关内容总结
1.Memcached常用命令总结 Memcached命令格式一般为: command 其中描述如下: 参数 描述 command 操作命令,一般为set/add/replace/get/delete ...