【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坐标,% ...
随机推荐
- 谷歌浏览器 查看源码里的a:hover CSS样式 还有火狐的查看方式
谷歌浏览器查看hover css样式 直接实例: 选中a标签 然后右侧面板 出现 箭头 点击 出现Toggle Element state提示 下面再hover前打勾 直接就可以显示效果了 浏览器查看 ...
- Python之闭包
Python之闭包 我们知道,在装饰器中,可以在函数体内创建另外一个函数,例如: def makebold(fn): def wrapped(): return "<b>&quo ...
- Python之路【第四篇】:模块
什么是模块: 模块就是一个功能的集合. 模块就和乐高积木差不多,你用这些模块组合出一个模型,然后也可以用这个模块加上其他的模块组合成一个新的模型 模块的种类: 1.内置模块(python自带的比如os ...
- Yii2-admin RBAC权限管理的实现
原文地址:http://www.open-open.com/lib/view/open1434638805348.html http://wlzyan.blog.163.com/blog/stat ...
- python快排算法
通过一趟排序将要排序的数据分割成独立的两部分,其中一部分的所有数据都比另外一部分的所有数据都要小,然后再按此方法对这两部分数据分别进行快速排序,整个排序过程可以递归进行,以此达到整个数据变成有序序列. ...
- 【8-17】HTML测试
Source : http://www.w3school.com.cn/html HTML 测验 结果:9/20 您的回答: 1.HTML 指的是? 您的回答:超文本标记语言(Hyper Text M ...
- 查看mysql数据库的数据引擎
1, SHOW VARIABLES LIKE 'storage_engine'; 2,show table status from 数据库库名 where name='表名',例: mysql> ...
- 重读C#委托、事件有感
去年学习C#的时候按照进度把C#的委托和事件“认真”的学习了一下,自己知道委托和事件的重要性,所以也努力的学习,可说实也没怎么学懂.碰巧这段时间在做一个解析GPS数据的小项目,因为其中有需要条件自动判 ...
- 如何让网页在浏览器标题栏显示自己制作的图标ico
第一步,制作一个尺寸16x16大小的PNG图片,可以用photoshop等图片处理工具来设计,然后保存到本地电脑上,通过ico在线制作或使用IconWorkshop工具制作ICO图标,ico图标命名为 ...
- C#高级知识点01---委托和事件
委托和事件 什么是委托? 简单来说,就是能把方法当作参数传递的对象,而且还知道怎么去调用这个方法,同时还约束了方法的签名. 例子: 用委托实现插件式编程: 1.