LeetCode刷题(40)--Search a 2D Matrix
来源:互联网 发布:淘宝上买手机 编辑:程序博客网 时间:2024/06/05 21:54
已经排好序的矩阵,搜索一个元素,二分法
class Solution(object): def searchMatrix(self, matrix, target): """ :type matrix: List[List[int]] :type target: int :rtype: bool """ m = len(matrix) if not m: return False n = len(matrix[0]) if not n: return False left = 0 right = m * n - 1 if matrix[left/n][left%n] > target: return False if matrix[right/n][right%n] < target: return False while left < right: mid = (left + right) / 2 if target < matrix[mid/n][mid%n]: right = mid - 1 elif target > matrix[mid/n][mid%n]: left = mid + 1 else : return True if matrix[left/n][left%n] == target: return True else: return False
阅读全文
0 0
- LeetCode刷题(40)--Search a 2D Matrix
- 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
- python基础 列表
- MNIST数据集入门
- 在手机上访问网站时自动跳转到网站mobile或者wap版页面
- .net需要掌握的工具
- keil 帮助工具使用
- LeetCode刷题(40)--Search a 2D Matrix
- C语言练习:百鸡百钱问题
- vue_的基本格式
- Java中判断字符串是否为数字的几种方法
- git 安装后的init、key、clone
- 网络请求哪家强
- easyui实现关闭当前窗口,所有窗口,其他窗口
- Python容器Containers
- Android UI界面中短暂消息提示实现