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的更多相关文章

  1. Leetcode74. Search a 2D Matrix搜索二维矩阵

    编写一个高效的算法来判断 m x n 矩阵中,是否存在一个目标值.该矩阵具有如下特性: 每行中的整数从左到右按升序排列. 每行的第一个整数大于前一行的最后一个整数. 示例 1: 输入: matrix ...

  2. LeetCode 74. 搜索二维矩阵(Search a 2D Matrix)

    74. 搜索二维矩阵 74. Search a 2D Matrix 题目描述 编写一个高效的算法来判断 m x n 矩阵中,是否存在一个目标值.该矩阵具有如下特性: 每行中的整数从左到右按升序排列. ...

  3. [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 ...

  4. [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 ...

  5. 【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 ...

  6. 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 ...

  7. [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 ...

  8. 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 ...

  9. LeetCode Search a 2D Matrix II

    原题链接在这里:https://leetcode.com/problems/search-a-2d-matrix-ii/ Write an efficient algorithm that searc ...

随机推荐

  1. 使用koa-body中间件后DELETE请求中ctx.request.body内容为空

    gitbook浏览此随笔 出现场景 在使用koa-body 做文件上传的时候,发现使用DELETE请求时,request.body中的内容为空对象{} app.js //code... const K ...

  2. macbook双网卡路由

    用route命令可以解决,步骤如下:1)确定你内网的网段,如果有多个都一一列出来,例如:192.168.1.0/24,10.20.0.0/16等 2)确定你内网网卡的网关IP,通过netstat -r ...

  3. 解决底部Button遮挡ListView最后一项内容的bug

    项目中ListView和Button经常是一起使用的,用ListView来展示数据,用Button来提交修改的数据或对修改的数据进行确定操作. 假如使用线性布局的话ListView会盖住整个Butto ...

  4. mysql查询某个字段并修改

    比如我存储的数据,有的是 山东,有的是山东省 我想统一改为山东省 UPDATE t_security SET province = REPLACE( province, '山东', '山东省' ) W ...

  5. 2019.9.28 csp-s模拟测试54 反思总结

    咕咕咕的冲动如此强烈x T1x: 看完题目想了想,感觉把gcd不为1的强行放在一组,看作一个连通块,最后考虑连通块之间的组合方式就可以了. 然后维护这个连通块可以写并查集可以连边跑dfs怎么着都行… ...

  6. IDEA maven项目查自动查看依赖关系,解决包冲突问题

    在maven项目中找到pom.xml,打开. <dependencies> <dependency> <groupId>org.apache.storm</g ...

  7. Primitive Topology

    原文:Primitive Topology 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/u010333737/article/details/78 ...

  8. 深入浅出Cocoa之类与对象【转】

    最近打算写一些ObjC中比较底层的东西,尤其是 runtime 相关的.苹果已经将 ObjC runtime 代码开源了,我们可以从:http://opensource.apple.com/sourc ...

  9. php 如何生成静态页

    PHP文件名:dome.php <?php $string = 1; ob_start(); @readfile("templets/list.html"); $text = ...

  10. 【JZOJ4793】【GDOI2017模拟9.21】妮厨的愤怒

    题目描述 栋栋和标标都是厨力++的妮厨.俗话说"一机房不容二厨",他们两个都加入了某OI( )交流♂( )群,在钦定老婆的时候出现了偏差,于是闹得不可开交.可是栋栋是群内的长者,斗 ...