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.

思路:此题还是比較简单的。主要是用两个二分法,才对第一列使用,再对特定的行使用。

详细代码例如以下:

public class Solution {
public boolean searchMatrix(int[][] m, int target) {
//两个二分搜索
//先搜索第一列,找到确定的行,然后再搜索行
int i = 0;
int j = m.length-1;
int mid = 0;
//搜寻第一列
while(i <= j){
mid = (i + j)/2;
if(m[mid][0] == target){
return true;
}else if(m[mid][0] < target){
i = mid + 1;
}else{
j = mid - 1;
}
}
if(m[mid][0] > target){
if(mid == 0){
return false;
}
mid--;//mid-1
}
//搜寻mid行
i = 0;
j = m[0].length -1;
int k = mid;
//搜寻到了返回true
while(i <= j){
mid = (i + j)/2;
if(m[k][mid] == target){
return true;
}else if(m[k][mid] < target){
i = mid + 1;
}else{
j = mid - 1;
}
}
//没有搜寻到,返回false
return false;
}
}

leetCode 74.Search a 2D Matrix(搜索二维矩阵) 解题思路和方法的更多相关文章

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

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

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

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

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

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

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

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

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

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

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

  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. 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. PHP CURL抓取网页 simple_html_dom类

    抓取网页数据后 数据录入到discuz中 <?php include('simple_html_dom.php'); function urlText(){ $url = 'http://www ...

  2. Java 精度控制

    四舍五入,保留两位小数 (找了很多种方法,都有问题,测试得出下面这种方式是可用的) String str="0.235"; String.format("%.2f&quo ...

  3. CentOS7.5 AndroidStudio Debug报错:insufficient permissions for device

    / ::: Launching instantapp $ adb push /home/vevi/AndroidStudioProjects/WeChatGod/app/build/outputs/a ...

  4. 判断wifi是2.4G还是5G

    1.WifiInfo 源码: int mFrequency=wifiInfo.getFrequency(); /** * @hide * TODO: makes real freq boundarie ...

  5. hibernate_06_单表操作_组件属性

    什么是组件属性? 比如address是students的其中一个属性,而address又有三个属性:邮编.电话和地址.address就是hibernate的组件属性. 首先建立Address类: pa ...

  6. QS之force(3)

    Example in project - First force signals in certain time and then noforce signals after some time. # ...

  7. Linux 帮助与语言设置以及(\)

    1.命令太长可以用反斜杠(\)来转义回车键,使用命令连续到下一行.注意:反斜杠后就立刻接着特殊字符才能转义. 2.修改语系为英文 LANG=en_US.utf8 export LC ALL=en_US ...

  8. 初识cocos creator的一些问题

    本文的cocos creator版本为v1.9.01.color赋值cc.Label组件并没有颜色相关的属性,但是Node有color的属性. //如果4个参数,在ios下有问题let rgb = [ ...

  9. vue中eventbus 多次触发的问题

    相比于react,vue是一个更上手很快的框架.所说简单易用,但在做Vue项目的过程中,就会发现,坑还是有的.组件之间传递数据是一个常见的需求,父子组件传递数据在官网有介绍,除了父子组件,一般地,任意 ...

  10. 团体程序设计天梯赛-练习集-L1-040. 最佳情侣身高差

    L1-040. 最佳情侣身高差 专家通过多组情侣研究数据发现,最佳的情侣身高差遵循着一个公式:(女方的身高)×1.09=(男方的身高).如果符合,你俩的身高差不管是牵手.拥抱.接吻,都是最和谐的差度. ...