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.

思路:先利用每一行的最后一个数与target判断,确定target可能在的行数;再在确定的那一行进行折半查找。

public class S074 {
public boolean searchMatrix(int[][] matrix, int target) {
int i = 0;
for (;i<matrix.length;i++) {
if (matrix[i][matrix[0].length-1]>=target) {
break;
}
}
if (i == matrix.length)
return false;
int l = 0, r = matrix[0].length-1;
//折半查找
while (l <= r) {
if (target == matrix[i][(l+r)/2])
return true;
else if (target > matrix[i][(l+r)/2])
l = (l+r)/2+1;
else
r = (l+r)/2-1;
}
return false;
}
}

Leetcode 074 Search a 2D Matrix的更多相关文章

  1. Java for LeetCode 074 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] 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 ...

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

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

  4. [Leetcode Week13]Search a 2D Matrix

    Search a 2D Matrix 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/search-a-2d-matrix/description/ D ...

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

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

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

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

随机推荐

  1. RedisDesktopManager

    下载地址: https://github.com/uglide/RedisDesktopManager/releases

  2. Jquery 操作DropDownList 根据条件选中

    $("#<%=DDLCounty.ClientID%> option").each(function () { if ($(this).text() == $(&quo ...

  3. ajax无法跳转页面的问题,

    将return true去掉!

  4. ajax加php实现简单的投票效果

    废话少说,作为一个前端猿,首先上前端的代码. 1.上html代码: <!DOCTYPE html> <html> <head lang="en"> ...

  5. HDU 5903 Square Distance

    $dp$预处理,贪心. 因为$t$串前半部分和后半部分是一样的,所以只要构造前一半就可以了. 因为要求字典序最小,所以肯定是从第一位开始贪心选择,$a,b,c,d,...z$,一个一个尝试过去,如果发 ...

  6. CSU 1811 Tree Intersection

    莫队算法,$dfs$序. 题目要求计算将每一条边删除之后分成的两棵树的颜色的交集中元素个数. 例如删除$u->v$,我们只需知道以$v$为$root$的子树中有多少种不同的颜色(记为$qq$), ...

  7. JDK根目录介绍

    /bin 存放可执行程序(编译器javac.exe 运行器java.exe 文档生成器javadoc.exe等 ). /db  小型数据库文件. /jre JRE. /include 形成jdk的c. ...

  8. scrapy 抓取动态网页

    -- coding: utf-8 -- ''' gouwu.sogou.com Spider, Created on Dec, 2014 version: 1.0 author: chenqx @ht ...

  9. moodle笔记之-权限api

    <?php//权限定义$capabilities = array( 'mod/mytest:managefiles' => array(//具体的权限:插件类型/插件名/权限 这里是增加一 ...

  10. 矩阵赋值实例(matrixAssign)

    题目:给一个二维数组赋值. 分析:主机端代码完成的主要功能: 启动CUDA,使用多卡时应加上设备号,或使用cudaSetDevice()设置GPU设备. 为输入数据分配内存空间 初始化输入数据 为GP ...