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.

Example 1:

Input:
matrix = [
[1, 3, 5, 7],
[10, 11, 16, 20],
[23, 30, 34, 50]
]
target = 3
Output: true

Example 2:

Input:
matrix = [
[1, 3, 5, 7],
[10, 11, 16, 20],
[23, 30, 34, 50]
]
target = 13
Output: false
class Solution {
public boolean searchMatrix(int[][] matrix, int target) {
if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
return false;
}
int row = matrix.length, col = matrix[0].length;
int start = 0, end = row * col - 1;
while (start <= end) {
int mid = start + (end - start) / 2;
int curRow = mid / col;
int curCol = mid % col;
if (matrix[curRow][curCol] == target) {
return true;
} else if (matrix[curRow][curCol] < target) {
start = mid + 1;
} else {
end = mid - 1;
}
}
return false;
}
}

[LC] 74. Search a 2D Matrix的更多相关文章

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

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

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

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

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

  4. 【LeetCode】74. Search a 2D Matrix 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 左下或者右上开始查找 顺序查找 库函数 日期 题目地 ...

  5. 74. Search a 2D Matrix

    题目: Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the f ...

  6. 【LeetCode】74. Search a 2D Matrix

    Difficulty:medium  More:[目录]LeetCode Java实现 Description Write an efficient algorithm that searches f ...

  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. 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. “杀死”纸质名片!HiHello能重构商业关系网吗?

    在当下的互联网时代,要添加好友去扩大自己的社交圈似乎是再简单不过.随便点击一个微信名片.与其他网友互相关注微博等,好像就又搭建了一个社交节点.暂且不讨论这些好友关系的质量问题,单是这样的方式并不适合于 ...

  2. 1811 06 pygame 的继续开发

    早上看了  高数和python   好像系统没有保存  桑心啊 关于游戏背景的制作 游戏背景就是    背景在移动  而主人物还在原位置的    常常用于跑酷游戏类  背景开始绘制两张图像  一张完全 ...

  3. shell 实现war包的配置更新和自动发布

    此脚本主要用来实现非maven tomcat项目的war包手动发布, 1.将测试war包上传至指定目录 2.备份目前生产代码 3.自动配置文件替换 4.新版本代码的发布 #!/bin/bash ### ...

  4. 一个或多个实体的验证失败。有关详细信息,请参阅“EntityValidationErrors”属性

    自定义异常类:FormattedDbEntityValidationException public class FormattedDbEntityValidationException : Exce ...

  5. 《Docekr入门学习篇》——Docker镜像制作

    Docker镜像制作 Docker镜像的构建分为两种,一种是手动构建,一种是dockerfile(自动构建) 手动构建 基于centos镜像进行构建制作Nginx镜像 [root@rbtnode1 ~ ...

  6. Java线程——synachronized关键字的作用(一)

    在并发编程中,多线程同时并发访问的资源叫做临界资源,当多个线程同时访问对象并要求操作相同资源时,分割了原子操作就有可能出现数据的不一致或数据不完整的情况,为避免这种情况的发生,我们会采取同步机制,以确 ...

  7. Java线程——线程习题(一)子线程执行10次后,主线程再运行5次,这样交替执行三遍

    题目:子线程执行10次后,主线程再运行5次,这样交替执行三遍 代码如下: package com.itheima.gan; /** * 子线程执行10次后,主线程再运行5次,这样交替执行三遍 * @a ...

  8. matlab画图中的坐标轴设置

    ax = gca; ax是个结构体,查看ax变量,可以看到所有可设置的属性.几个常见属性如下: 设置坐标轴字体大小,粗细,字体名 2014b之后版本: ax = gca; ax.FontSize = ...

  9. 吴裕雄--天生自然 PYTHON3开发学习:元组

    tup1 = ('Google', 'Runoob', 1997, 2000) tup2 = (1, 2, 3, 4, 5, 6, 7 ) print ("tup1[0]: ", ...

  10. 编程基础-servlet1

    1.Servelet是什么 sevlet是Server与Applet 的缩写,即服务端小程序.Sun公司提供的开发动态web资源的技术. servelet本质是java类,但遵循Servlet规范,没 ...