leetcode 【Search a 2D Matrix 】python 实现
题目:
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.
代码:oj测试通过 Runtime: 75 ms
class Solution:
# @param matrix, a list of lists of integers
# @param target, an integer
# @return a boolean
def searchInline(self, line, target):
start = 0
end = len(line)-1
while start <= end :
if start == end :
return [False,True][line[start]==target]
if start+1 == end :
if line[start]==target or line[end]==target :
return True
else:
return False
mid=(start+end)/2
if line[mid]==target :
return True
elif line[mid]>target :
end = mid-1
else :
start = mid+1 def searchMatrix(self, matrix, target):
if len(matrix) == 0 :
return False if len(matrix) == 1 :
return self.searchInline(matrix[0], target) if len(matrix) == 2 :
return self.searchInline([matrix[1],matrix[0]][matrix[1][0]>target], target)\ start = 0
end = len(matrix)-1
while start <= end :
if start == end:
return self.searchInline(matrix[start],target)
if start+1 == end:
if matrix[start][0] <= target and matrix[end][0] > target:
return self.searchInline(matrix[start],target)
if matrix[end][0] < target :
return self.searchInline(matrix[end],target)
mid = (start+end+1)/2
if matrix[mid][0] <= target and matrix[mid+1][0] > target:
return self.searchInline(matrix[mid],target)
elif matrix[mid][0] > target :
end = mid-1
else :
start = mid+1
思路:
先按行二分查找,再按列二分查找。
代码写的比较繁琐。
leetcode 【Search a 2D Matrix 】python 实现的更多相关文章
- [leetcode]Search a 2D Matrix @ Python
原题地址:https://oj.leetcode.com/problems/search-a-2d-matrix/ 题意: Write an efficient algorithm that sear ...
- [LeetCode] Search a 2D Matrix II 搜索一个二维矩阵之二
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...
- [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 ...
- LeetCode Search a 2D Matrix II
原题链接在这里:https://leetcode.com/problems/search-a-2d-matrix-ii/ Write an efficient algorithm that searc ...
- 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 ...
- LeetCode -- Search a 2D Matrix & Search a 2D Matrix II
Question: Search a 2D Matrix Write an efficient algorithm that searches for a value in an m x n matr ...
- 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 ...
- [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 ...
- [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 ...
- LeetCode Search a 2D Matrix II (技巧)
题意: 有一个矩阵,每行有序,每列也有序.判断一个数target是否存在于此矩阵中. 思路: 从右上角开始,如果当前数字<target,则该行作废.若当前数字>target,该列作废.这样 ...
随机推荐
- 【MFC】将当前的日期转化为1970年开始的秒计数
CTime time1 = CTime::GetCurrentTime(); int nTSeconds = time1.GetTime(); CTime time2(,,,,,); nTSecond ...
- ADO.NET #3-1 (GridView + DataReader + SqlCommand)完全手写Code Behind
[C#] ADO.NET #3-1 (GridView + DataReader + SqlCommand)完全手写.后置程序代码 之前有分享过一个范例 [C#] ADO.NET #3 (GridVi ...
- java Vamei快速教程04 封装和接口
作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 总结之前的内容,对象(object)指代某一事物,类(class)指代象的类型.对 ...
- 初见微服务之RESTful API
1. REST名称由来 REST全称为Representational State Transfer,即表述性状态转移,最早由Roy Feilding博士在世纪之交(2000年)提出,喜欢追根溯源的朋 ...
- 完结篇OO总结
目录 前言 一.第四单元两次架构设计 二.架构设计及OO方法理解的演进 三.测试理解与实践的演进 四.课程收获 五.改进建议 前言 持续了17周的OO终于走向了尾声,想想寒假的时候连类都不知道是什么, ...
- C# 接口慨述
接口(interface)用来定义一种程序的协定.实现接口的类或者结构要与接口的定义严格一致.有了这个协定,就可以抛开编程语言的限制(理论上).接口可以从多个基接口继承,而类或结构可以实现多个接口.接 ...
- 03_15_interface
03_15_interface 1. 接口 接口是抽象方法和常量值的定义的集合. 从本质上讲,接口是一种特殊的抽象类,这种抽象类中只包含常量和方法的定义,而没有变量和方法的实现. 2. 接口特性 接口 ...
- 关于小程序 scroll-view中设置scroll-top无效 和小说图书阅读进度条小案例
在最近的项目有做到关于小说阅读的进度条功能,其中用到scroll-view和slider组件,发现scroll-view中的scroll-top在设置值后无效,出现这种情况大概是以下几种问题: 1.s ...
- 【Ecshop】修改处理用户购物车的行为
Ecshop v2.7.3的购物车处理方面在现在看来有比较反用户体验的设计: 用户未登录时加入购物车的商品,在用户登录后会被清空而不是加入到登录用户的购物车中: 用户登录后加入购物车的商品,在退出后会 ...
- 解决cmd目录下pip命令不存在的问题
解决cmd目录下pip命令不存在的问题 注:pip.exe程序在Python安装目录下的scripts中1.在cmd命令中输入: 先输入:python -m ensurepip 再输入:python ...