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. POJ 1172 DFS

    (感谢wzc学长的幻灯片) 单组数据 注意从必经点能到标记过的点则此点不是分裂点. //By: Sirius_Ren #include <cstdio> #include <queu ...

  2. Spring Boot (14) 数据源配置原理

    数据源配置源码 这里截取org.springframework.boot.autoconfigure.jdbc.DataSourceConfiguration的部分源码,主要介绍Tomcat和Hika ...

  3. APP开发中的弹窗体系,UI设计师不能忽视的地方

    1. 弹窗的定义 弹窗分为模态弹窗和非模态弹窗两种. 弹窗分类 模态弹窗:很容易打断用户的操作行为,用户必须回应,否则不能进行其他操作. 非模态弹窗:不会影响用户的操作,用户可以不对其进行回应,非模态 ...

  4. Linux系统下通过命令行对mysql数据进行备份和还原

    一.备份 1.进入mysql目录 cd /var/lib/mysql (进入mysql目录,根据安装情况会有差别) 2.备份 mysqldump -u root -p密码 数据库名 数据表名 > ...

  5. Oracle SQL 性能优化技巧

    Select语句完整的执行顺序: SQL Select语句完整的执行顺序: 1. from子句组装来自不同数据源的数据: 2.where子句基于指定的条件对记录行进行筛选: 3.group by子句将 ...

  6. C# Socket发送接收字节数组和十六16进制之间转换函数

    近期在使用远程网络模块的时候, 需要用的Socket发送数据,远程模块指令为16进制. 官方提供的DEMO比较繁琐.不方便新手使用. 下面的转换函数可大大方便新手使用. // 16进制字符串转字节数组 ...

  7. MyEclipse 连接Oracle数据库(初学者必看)

    前言:刚接触Oracle数据库,便有一个需求,编写控制台程序,实现主人登录.数据库为Oracle.下面详细介绍一下MyEclipse 连接Oracle数据库.   package DbHelp; im ...

  8. 利用string 字符串拷贝

    序言:对于laws的代码,完全从Matlab中转来.其中用到了字符串复制和对比的函数. C++要求: 输入字符串,根据字符串,来确定选择数组,用于下一过程 MatLab代码: (1).文件calLaw ...

  9. Ubuntu 16.04安装和卸载软件命令

    安装软件 apt-get install softname1 softname2 softname3…… 卸载软件 apt-get remove softname1 softname2 softnam ...

  10. monkey测试环境搭建 及 操作步骤

    1.环境搭建 a.下载安卓SDK 链接:https://pan.baidu.com/s/1-OB6UVPvl5-N-vFdykfMmA 提取码:3spx b.配置环境变量(配置完成,重启系统,配置生效 ...