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 ...
随机推荐
- Flask初学者:URL(传参,请求,重定向)
URL传参: 良好的URL:视图函数对应的url以/结尾是一种良好url,因为用户在访问的时候无论他有没有加上最后这个斜杠,都是能访问到的,相反,视图函数的url没有以/结尾,用户访问的时候却加上了这 ...
- linux 服务器被植入ddgs、qW3xT.2挖矿病毒处理记录
被入侵后的现象: 发现有qW3xT.2与ddgs两个异常进程,消耗了较高的cpu,kill掉后 过一会就会重新出现. kill 掉这两个异常进程后,过一段时间看到了如下进程: 首先在/etc/sysc ...
- Selenium与PhantomJS【转】
爬虫(Spider),反爬虫(Anti-Spider),反反爬虫(Anti-Anti-Spider) 之间恢宏壮阔的斗争... Day 1 小莫想要某站上所有的电影,写了标准的爬虫(基于HttpCli ...
- Linux命令之----tree
命令简介 tree命令的中文意思为“树”,功能是以树形结构列出指定目录下的所有内容,包括所有文件.子目录及子目录里的目录和文件. 命令格式 tree [option] [directory]tree ...
- Word 2013发布博客测试
Hello world ! I am from word2013! 测试修改 这里添加一行文字. 参考 1在 Word 中建立博客的相关帮助 2使用Word2013发布随笔到博客园 PS: 参考2 ...
- 47.关于gradle的解疑
Short Answer Gradle is a build system. Long Answer Before Android Studio you were using Eclipse for ...
- Mac OS X 上的Apache配置
Mac系统自带apache服务器 查看apache版本 sudo apachectl -v 启动apache sudo apachectl start 重启apache sudo apachectl ...
- 前端应该掌握的CSS实现多列等高布局
1.引言 我们在写页面的时候,有的时候会遇到多栏布局,每个栏目里面的内容有的时候可能不一样,这样就会导致每个栏目实际的高度也是不一样的,如果每个栏目有背景颜色的,就会导致每个栏目的底部是对不齐的,用户 ...
- Windows下Git多账号ssh-key(复制自己用)
Windows下Git多账号配置,同一电脑多个ssh-key的管理 这一篇文章是对上一篇文章<Git-TortoiseGit完整配置流程>的拓展,所以需要对上一篇文章有所了解,当然直接往下 ...
- centos7下使用git
问:为什么需要版本控制系统?[转:http://www.cnblogs.com/shenliang123/p/3824383.html] 版本控制是一种记录若干文件内容变化,以便将来查阅特定版本修订情 ...