LeetCode74 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
. (Medium)
分析:
考察题目给的二维数组的特点发现,其本质跟一个排好序的一维数组没有区别,所以就是一个二分查找问题。
对于mid,其对应的点是matrix[mid / n][mid % n]
代码:
class Solution {
public:
bool searchMatrix(vector<vector<int>>& matrix, int target) {
int m = matrix.size(), n = matrix[].size();
int start = , end = m * n - ;
while (start + < end) {
int mid = start + (end - start) / ;
if (matrix[mid / n][mid % n] == target) {
return true;
}
else if (matrix[mid / n][mid % n] < target) {
start = mid;
}
else {
end = mid;
}
}
if (matrix[start / n][start % n] == target) {
return true;
}
if (matrix[end / n][end % n] == target) {
return true;
}
return false;
}
};
LeetCode74 Search a 2D Matrix的更多相关文章
- Leetcode74. Search a 2D Matrix搜索二维矩阵
编写一个高效的算法来判断 m x n 矩阵中,是否存在一个目标值.该矩阵具有如下特性: 每行中的整数从左到右按升序排列. 每行的第一个整数大于前一行的最后一个整数. 示例 1: 输入: matrix ...
- LeetCode 74. 搜索二维矩阵(Search a 2D Matrix)
74. 搜索二维矩阵 74. Search a 2D Matrix 题目描述 编写一个高效的算法来判断 m x n 矩阵中,是否存在一个目标值.该矩阵具有如下特性: 每行中的整数从左到右按升序排列. ...
- [LeetCode] Search a 2D Matrix II 搜索一个二维矩阵之二
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 搜索一个二维矩阵
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 ...
- 54. Search a 2D Matrix && Climbing Stairs (Easy)
Search a 2D Matrix Write an efficient algorithm that searches for a value in an m x n matrix. This m ...
- [CareerCup] 11.6 Search a 2D Matrix 搜索一个二维矩阵
11.6 Given an M x N matrix in which each row and each column is sorted in ascending order, write a m ...
- Search a 2D Matrix | & II
Search a 2D Matrix II Write an efficient algorithm that searches for a value in an m x n matrix, ret ...
- LeetCode Search a 2D Matrix II
原题链接在这里:https://leetcode.com/problems/search-a-2d-matrix-ii/ Write an efficient algorithm that searc ...
随机推荐
- MVVM 一种新型架构框架
MVVM是Model-View-ViewModel的简写.微软的WPF带来了新的技术体验,如Silverlight.音频.视频.3D.动画……,这导致了软件UI层更加细节化.可定制化.同时,在技术层面 ...
- scanf("%c", &ch)和scanf(" %c", &ch)和scanf("%s", str)的注意事项
scanf("%c", &ch)和scanf(" %c", &ch): %c会读取回车和空格,所以一定要使用后者,即在%c前面加一个空格. %s ...
- Springmvc使用阿里巴巴的fastjson传输到前台中文乱码的解决方案,他娘的大家都少制造垃圾,学习过程将会多么快乐
弄了大概七八个小时吧 都他妈比的抄来抄去,一分一秒的去试错 最终参考这个问题解决了乱码的情况https://bbs.csdn.net/topics/392169549 412 需要在springmvc ...
- Linux下根目录root扩容
参考博客:https://blog.csdn.net/qq_36527339/article/details/81772996 1.首先虚拟机关机 —> 选中要扩容的虚拟机 —>编辑虚拟机 ...
- leetcode 839 Similar String Groups
题目 Two strings X and Y are similar if we can swap two letters (in different positions) of X, so that ...
- MyBatis中动态加载where条件
1.trim 2.where 1 = 1 where 1=1 <if test="beginDate !=null and beginDate !='' "> and ...
- win10 ubuntu 双系统启动顺序设置
之前安装ubuntu的时候就遇到过这个问题, 当时解决了,设置成开始可以选择启动ubuntu系统还是win系统. 但是过了好久后又忘记了,最近win10开了一次安全模式启动后,一开机就是win10,u ...
- 【模板】矩阵快速幂 洛谷P2233 [HNOI2002]公交车路线
P2233 [HNOI2002]公交车路线 题目背景 在长沙城新建的环城公路上一共有8个公交站,分别为A.B.C.D.E.F.G.H.公共汽车只能够在相邻的两个公交站之间运行,因此你从某一个公交站到另 ...
- CF981H K Paths
CF981H K Paths 题解 一道不错的分治ntt题目 题目稍微转化一下,就是所有k条链的存在交,并且交的部分都被覆盖k次 所以一定是两个点,之间路径选择k次,然后端点两开花 f[x]表示x子树 ...
- IO流1 --- File类的实例化 --- 技术搬运工(尚硅谷)
构造器1 File(String pathname) //相对路径 File file1 = new File("hello.txt"); //windows绝对路径 File f ...