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 in ascending from left to right.
  • Integers in each column are sorted in ascending from top to bottom.

Example:

Consider the following matrix:

[
[1, 4, 7, 11, 15],
[2, 5, 8, 12, 19],
[3, 6, 9, 16, 22],
[10, 13, 14, 17, 24],
[18, 21, 23, 26, 30]
]

Given target = 5, return true.

Given target = 20, return false.

这个题目思路, 1. brute force  T: O(m*n)    2. T: O(m+n)     3. T: O(lg(n!))  先找第一行, 第一列, 2n 再从(1,1) 开始扫第二行第二列, 2(n-1), .... 所以是lgn + lg (n-1) + lg(n-2)...lg(1) = lg(n!)

1) T: O(m*n)

两个for loop即可.

2) T: O(m + n)

        #O(n + m) , O(1)
if not matrix or len(matrix[0]) == 0: return False
lr, lc = len(matrix), len(matrix[0])
r, c = lr - 1, 0
while r >= 0 and c < lc:
if matrix[r][c] > target:
r -= 1
elif matrix[r][c] < target:
c += 1
else:
return True
return False

3)  T: O(lg(n!))

        # T: O(lg(n!))   S: O(1)
def helper(i, flag):
l = i
r = lrc[0]-1 if flag == 'row' else lrc[1] -1if flag == 'col':
while l + 1 < r:
mid = l + (r - l)//2
if matrix[i][mid] > target:
r = mid
elif matrix[i][mid] < target:
l = mid
else:
return True
if target in [matrix[i][l], matrix[i][r]]:
return True
return False
else:
while l + 1 <r:
mid = l + (r - l)//2
if matrix[mid][i] > target:
r = mid
elif matrix[mid][i] < target:
l = mid
else:
return True
if target in [matrix[l][i], matrix[r][i]]:
return True
return False if not matrix or len(matrix[0]) == 0: return False
lrc = [len(matrix), len(matrix[0])]
for i in range(min(lrc)):
row_check = helper(i, "row")
col_check = helper(i, "col")
if row_check or col_check: return True
return False

[LeetCode] 240. Search a 2D Matrix II_Medium tag: Binary Search的更多相关文章

  1. [LeetCode] 33. Search in Rotated Sorted Array_Medium tag: Binary Search

    Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e. ...

  2. [LeetCode] 374. Guess Number Higher or Lower_Easy tag: Binary Search

    We are playing the Guess Game. The game is as follows: I pick a number from 1 to n. You have to gues ...

  3. [LeetCode] 278. First Bad Version_Easy tag: Binary Search

    You are a product manager and currently leading a team to develop a new product. Unfortunately, the ...

  4. Leetcode之二分法专题-240. 搜索二维矩阵 II(Search a 2D Matrix II)

    Leetcode之二分法专题-240. 搜索二维矩阵 II(Search a 2D Matrix II) 编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target.该矩阵 ...

  5. LeetCode 240. 搜索二维矩阵 II(Search a 2D Matrix II) 37

    240. 搜索二维矩阵 II 240. Search a 2D Matrix II 题目描述 编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target.该矩阵具有以下特性 ...

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

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

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

  8. 【LeetCode】240. Search a 2D Matrix II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  9. 【LeetCode】240. Search a 2D Matrix II

    Search a 2D Matrix II Write an efficient algorithm that searches for a value in an m x n matrix. Thi ...

随机推荐

  1. 如何去除Launcher默认的google search bar?

    JB2/JB3/JB5/JB9版本: 1. 请修改 Launcher2/res/layout/qsb_bar.xml,如下:<include android:id="@+id/qsb_ ...

  2. Python数据结构———栈

    线性数据结构 当添加一个项目时,它就被放在这样一个位置:在之前存在的项与后来要加入的项之间.像这样的数据集合常被称为线性数据结构. 栈 栈是一个项的有序集合.添加项和移除项都发生在同一“端”,这一端通 ...

  3. Fis3构建迁移Webpack之路

    Webpack从2015年9月第一个版本横空初始至今已逾2载.它的出现,颠覆了一大批主流构建如Ant.Grunt和Gulp等等.腾讯NOW直播IVWEB团队之前一直采用Fis构建,本篇文章主要介绍从F ...

  4. 不同的GCD算法

    分类: C语言程序2014-10-08 15:10 28人阅读 评论(0) 收藏 举报 gcdC语言程序位运算 早在公元前300年左右,欧几里得就在他的著作<几何原本>中给出了高效的解法- ...

  5. [No0000145]深入浅出图解C#堆与栈 C# Heap(ing) VS Stack(ing)理解堆与栈2/4

    前言   虽然在.Net Framework 中我们不必考虑内在管理和垃圾回收(GC),但是为了优化应用程序性能我们始终需要了解内存管理和垃圾回收(GC).另外,了解内存管理可以帮助我们理解在每一个程 ...

  6. [No0000DA]WPF ControlTemplate简介

    一.简介 WPF包含数据模板和控件模板,其中控件模板又包括ControlTemplate和ItemsPanelTemplate,这里讨论一下ControlTemplate.其实WPF的每一个控件都有一 ...

  7. vue $set的使用

    在我们使用vue进行开发的过程中,可能会遇到一种情况:当生成vue实例后,当再次给数据赋值时,有时候并不会自动更新到视图上去: 当我们去看vue文档的时候,会发现有这么一句话:如果在实例创建之后添加新 ...

  8. Page9:结构分解以及系统内部稳定和BIBO稳定概念及其性质[Linear System Theory]

    内容包含系统能控性结构分解.系统能观测性结构分解以及系统结构规范分解原理,线性系统的内部稳定.BIBO稳定概念及其性质

  9. 转:Eclipse 各种小图标的含义

    原文地址:https://www.cnblogs.com/widget90/p/7592507.html Eclipse 各种小图标的含义,记录一下. Eclipse的Package Explorer ...

  10. python中dict的fromkeys用法

    fromkeys是创造一个新的字典.就是事先造好一个空字典和一个列表,fromkeys会接收两个参数,第一个参数为从外部传入的可迭代对象,会将循环取出元素作为字典的key值,另外一个参数是字典的val ...