40 leetcode - Search a 2D Matrix
来源:互联网 发布:ubuntu u盘版本 编辑:程序博客网 时间:2024/05/16 04:23
#!/usr/bin/python# -*- coding: utf-8 -*-'''Search a 2D Matrix 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.'''class Solution(object): def binary_search(self,l,length,target): if not l or length == 0: return False left,right = 0,length - 1 while left <= right: mid = (left + right)/2 if l[mid] == target: return True elif l[mid] > target: right = mid - 1 else: left = mid + 1 return False def searchMatrix(self, matrix, target): """ :type matrix: List[List[int]] :type target: int :rtype: bool """ row_length,col_length = len(matrix),len(matrix[0]) if row_length == 0 or col_length == 0: return False index = 0 while index < row_length: if matrix[index][0] <= target and matrix[index][col_length - 1] >= target: return self.binary_search(matrix[index],col_length,target) index += 1 return Falseif __name__ == "__main__": s = Solution() b = [[1, 3, 5, 7],[10, 11, 16, 20],[23, 30, 34, 50]] print s.searchMatrix(b,12)
0 0
- 40 leetcode - Search a 2D Matrix
- [leetcode][Search] Search a 2D Matrix
- LeetCode: Search a 2D Matrix
- LeetCode Search a 2D Matrix
- [Leetcode] Search a 2D Matrix
- LeetCode : Search a 2D Matrix
- [LeetCode] Search a 2D Matrix
- Leetcode 74 Search a 2D Matrix
- leetcode 90: Search a 2D Matrix
- [LeetCode]Search a 2D Matrix
- [Leetcode] Search a 2-D matrix
- [Leetcode]Search a 2D Matrix
- [leetcode]Search a 2D Matrix
- LeetCode-Search a 2D Matrix
- [leetcode] Search a 2D Matrix
- LeetCode - Search a 2D Matrix
- LeetCode:Search a 2D Matrix
- LeetCode 74: Search A 2D Matrix
- PLC编程再思考之二:SOA
- PLC编程再思考之三:面向过程
- Java 枚举enum 使用详解
- HDU 3555 Bomb(数位dp 深搜版)
- 微信开发者模式之网址接入TOKEN验证
- 40 leetcode - Search a 2D Matrix
- 灵活运用SearchView搜索框控件(二)
- 给自己一点正能量
- [BZOJ3388][Usaco2004 Dec]Cow Ski Area雪场缆车
- [乐意黎转载]git 常用命令
- linux查看端口被哪个进程占用
- 程序运行时的内存空间分布
- 获取当前连接的wifi的名字
- 51nod--基础题大数加法