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.


题目标签:Array

  题目给了我们一个有序的矩阵,让我们找target是否在矩阵中。首先我们分析,如果我们知道了target 在哪一行的话,直接用binary search 就可以了。所以第一步可以用binary search 来搜索第一列,确认target在哪一行。在用一次二分法对于那一行,找出target。

Java Solution:

Runtime beats 71.85%

完成日期:07/23/2017

关键词:Array

关键点:用二分法两次

 public class Solution
{
public boolean searchMatrix(int[][] matrix, int target)
{
if(matrix.length == 0 || matrix[0].length == 0)
return false;
if(target < matrix[0][0] || target > matrix[matrix.length-1][matrix[0].length-1])
return false;
// first determine which row the target is in
int top = 0;
int bottom = matrix.length-1; while(top <= bottom)
{
int mid = top + (bottom - top) / 2; if(matrix[mid][0] == target)
return true;
else if(matrix[mid][0] > target)
bottom = mid - 1;
else
top = mid + 1;
} int rowNum = bottom; // use binary search for this row
int left = 0;
int right = matrix[0].length-1; while(left <= right)
{
int mid = left + (right - left) / 2; if(matrix[rowNum][mid] == target)
return true;
else if(matrix[rowNum][mid] < target)
left = mid + 1;
else
right = mid - 1; } return false;
}
}

参考资料:

http://www.cnblogs.com/grandyang/p/4323301.html

LeetCode 算法题目列表 - LeetCode Algorithms Questions List

LeetCode 74. Search a 2D Matrix(搜索二维矩阵)的更多相关文章

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

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

  3. 074 Search a 2D Matrix 搜索二维矩阵

    编写一个高效的算法来搜索 m x n 矩阵中的一个目标值.该矩阵具有以下特性:    每行中的整数从左到右排序.    每行的第一个整数大于前一行的最后一个整数.例如,以下矩阵:[  [1,   3, ...

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

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

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

  6. leetcode 74. Search a 2D Matrix 、240. Search a 2D Matrix II

    74. Search a 2D Matrix 整个二维数组是有序排列的,可以把这个想象成一个有序的一维数组,然后用二分找中间值就好了. 这个时候需要将全部的长度转换为相应的坐标,/col获得x坐标,% ...

  7. [LeetCode] 74 Search a 2D Matrix(二分查找)

    二分查找 1.二分查找的时间复杂度分析: 二分查找每次排除掉一半不合适的值,所以对于n个元素的情况来说: 一次二分剩下:n/2 两次:n/4 m次:n/(2^m) 最坏情况是排除到最后一个值之后得到结 ...

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

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

  10. search a 2D matrix(在二维数组中搜索一个元素)

    Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...

随机推荐

  1. request.getParameter()获取URL中文参数乱码的解决办法

    这个问题耽误好长时间,URL传中文参数出现乱码,就算首次使用request接收就添加 request.setCharacterEncoding("UTf-8"); 依然报错不误. ...

  2. 纳税服务系统【自动受理,Quartz任务调度】

    需求 回到我们的需求: 自动投诉受理:在每个月月底最后一天对本月之前的投诉进行自动处理:将投诉信息的状态改为 已失效.在后台管理中不能对该类型投诉进行回复. 这个需求需求我们要怎么弄呢????要在每个 ...

  3. bookStore项目总结

    感想 该项目是目前为止,我写过代码量最多的项目了-..虽然清楚是没有含金量的[跟着视频来写的],但感觉自己也在进步中-.. 写的过程中,出了不少的问题-..非常多的Servlet,JSP看得眼花-.. ...

  4. Hibernate中的主键生成器generator

    本文讲述Hibernate的generator属性的意义.Generator属性有7种class,本文简略描述了这7种class的意义和用法. [xhtml] view plaincopy <c ...

  5. MongoDB中的映射,限制记录和记录拼排序 文档的插入查询更新删除操作

    映射 在 MongoDB 中,映射(Projection)指的是只选择文档中的必要数据,而非全部数据.如果文档有 5 个字段,而你只需要显示 3 个,则只需选择 3 个字段即可. find() 方法 ...

  6. 学习OpenResty的正确姿势

    前段时间老罗退出得到专栏事情闹得沸沸扬扬,另一位老罗也给出了合理的会员退费,感觉得到还是蛮贴心的.想想也是,毕竟精力有限,如今老罗也有了十亿的投资,集中精力做好手机才是主业.记得老罗刚开专栏那段时间很 ...

  7. 使用.net 自建短链接(短网址)

    短连接大家都不陌生,例如新浪的 t.cn .京东的 3.cn .淘宝的 tb.cn 等等.都已经是家喻户晓的短连接域名.不知道有多少人像我一样,对短连接原理好奇而且尝试自建了呢? 今天发布这个文章的目 ...

  8. 深入理解计算机系统chapter3

    栈在处理过程调用中起到至关重要的作用,栈向下增长,栈顶元素的地址是所有栈中元素最小的.栈指针%esp保存着栈顶元素的地址 控制: 重点: 基于条件数据传送的代码比基于条件控制转移(预测错误惩罚比较高) ...

  9. DotNetCore跨平台~linux上还原自主nuget包需要注意的问题

    问题的产生的背景 由于我们使用了jenkins进行部署(jenkins~集群分发功能和职责处理),而对于.net core项目来说又是跨平台的,所以对它的项目拉取,包的还原,项目的编译和项目的发布都是 ...

  10. EOutOfResources EConvertError is not a valid integer value Unable to insert a line

    is not a valid integer value???project Teclaser_Single.exe raised exception class EOutOfResources wi ...