leetcode -- Search a 2D Matrix II -- 重点要理解
2015-12-15 21:37
253 查看
https://leetcode.com/problems/search-a-2d-matrix-ii/
参考http://bookshadow.com/weblog/2015/07/23/leetcode-search-2d-matrix-ii/
外层循环枚举行,内层枚举列
O(m + n)复杂度,要好好看!!!!!,因为每一列is ascending order from top to bottom.
参考http://bookshadow.com/weblog/2015/07/23/leetcode-search-2d-matrix-ii/
思路1
从矩阵的右上角开始搜索外层循环枚举行,内层枚举列
O(m + n)复杂度,要好好看!!!!!,因为每一列is ascending order from top to bottom.
class Solution: # @param {integer[][]} matrix # @param {integer} target # @return {boolean} def searchMatrix(self, matrix, target): y = len(matrix[0]) - 1 for x in range(len(matrix)): while y and matrix[x][y] > target: y -= 1 if matrix[x][y] == target: return True return False
思路2
外层循环枚举行,内层二分搜索列相关文章推荐
- Java NIO使用及原理分析(二)
- 扣丁学堂——收发短信
- jquery.validate校验+jquery.form提交,配合使用
- Java出现No enclosing instance of type E is accessible. Must qualify the allocation with an enclosing
- [Coursera]算法基础_Week9_考试_Q2
- IC分词和IK分词
- 第4天
- cutils-trace(18431): Error opening trace file: No such file or directory
- 前端小工具集合
- 一个非常简单的例子告诉你attachEvent和addEventListener的区别
- LeetCode 2 Add Two Numbers(链表操作)
- 电。视。电。影。。。网。。。站
- 递归读取heritrix 爬下来的目录文件
- 数组和指针
- LeetCode 213:House Robber II
- 进击的KFC:UI(14)集合视图UICollectionView
- [HDU 1427]速度计算24点(DFS暴力搜索)
- 家庭作业汇总
- This Is The Rule That Follow
- Linux下安装Git