【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 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 row=matrix.size();
int col=matrix[].size();
vector<int> m(row*col);
for(int i=;i<row;i++)
{
for(int j=;j<col;j++)
{
m[i*col+j]=matrix[i][j];
}
}
int left=;
int right=m.size()-;
int mid;
while(left<=right)
{
mid=(left+right)/;
if(m[mid]>target)
right=mid-;
else if(m[mid]<target)
left=mid+;
else
return true;
}
return false;
}
};
class Solution {
public:
bool searchMatrix(vector<vector<int> > &matrix, int target) {
int row=matrix.size();
int col=matrix[].size();
int left=;
int right=row*col-;
int mid;
int m;
while(left<=right)
{
mid=(left+right)/;
m=matrix[mid/col][mid%col];
if(m>target)
right=mid-;
else if(m<target)
left=mid+;
else
return true;
}
return false;
}
};
【leetcode】Search a 2D Matrix的更多相关文章
- 【leetcode】 Search a 2D Matrix (easy)
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...
- 【Leetcode】【Medium】Search a 2D Matrix
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...
- 【数组】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] 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 Insert Position
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
- leetcode 74. Search a 2D Matrix 、240. Search a 2D Matrix II
74. Search a 2D Matrix 整个二维数组是有序排列的,可以把这个想象成一个有序的一维数组,然后用二分找中间值就好了. 这个时候需要将全部的长度转换为相应的坐标,/col获得x坐标,% ...
随机推荐
- MVC中的@section
在前文<MVC中 _ViewStart _Layout Index三个页面中的加载顺序> 中另外指定母版页 Layout.cshtml时...遇到了这个问题.. 报错: 以下各节已定义,但 ...
- XMLBEANS的使用总结
在本文中,我们将详细了解最好的数据对象XMLBean.从传统角度来说,在Java应用程序中使用XML,就是在从 XML文档向Java导入数据的技术或从数据模型层向XML导出数据技术之间架起了一座桥梁. ...
- ActiveMQ 目录
(1) ActiveMQ简介 (2) JMS基本概念和模型 (3) JMS的可靠性 (4) ActiveMQ结合Spring开发 (5)ActiveMQ支持的传输协议 (6)ActiveMQ消息存储持 ...
- 深入理解Javascript面向对象编程
深入理解Javascript面向对象编程 阅读目录 一:理解构造函数原型(prototype)机制 二:理解原型域链的概念 三:理解原型继承机制 四:理解使用类继承(继承的更好的方案) 五:建议使用封 ...
- 网络安全&信息安全&系统安全常用名词汇总
拖库(脱裤) 隐写术 拖库(脱裤) 隐写术 拖库(脱裤) 隐写术 拖库(脱裤) 隐写术
- ASP FORM表单提交判断
ASP提交表单是先进行Form填写检测,检测完成没问题之后再执行写入数据库表操作. 相关源码: <script language="javascript"> funct ...
- StarWind的安装配置
将安装包拷贝到本地后,执行以下exe文件,默认下一步,直到安装完成. 需汉化版本则将language_chinese.xml文件拷贝到以下路径: C:\Program Files\StarWind S ...
- 重温javascript事件机制
以前用过一段时间的jquery感觉太方便,太强大了,各种动画效果,dom事件.创建节点.遍历.控件及UI库,应有尽有:开发文档也很多,网上讨论的问题更是甚多,种种迹象表明jquery是一个出色的jav ...
- QT连接Linux mysql注意
windows: #define MYSQLDB "QMYSQL"#define MYSQLDB_HOSTNAME "192.168.228.168"#defi ...
- mysql explain用法和结果的含义
重点是第二种用法,需要深入的了解. 先看一个例子: mysql> explain select * from t_order; +----+-------------+---------+--- ...