[leetcode]Search a 2D Matrix @ Python
原题地址:https://oj.leetcode.com/problems/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:
# @param matrix, a list of lists of integers
# @param target, an integer
# @return a boolean
def searchMatrix(self, matrix, target):
i = 0; j = len(matrix[0]) - 1
while i < len(matrix) and j >= 0:
if matrix[i][j] == target: return True
elif matrix[i][j] > target: j -= 1
else: i += 1
return False
[leetcode]Search a 2D Matrix @ Python的更多相关文章
- [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,该列作废.这样 ...
随机推荐
- Orleans介绍
一.介绍 Orleans是一个框架,提供了一个直接的方法来构建分布式高规模计算应用程序 默认可扩展 -> Orleans处理构建分布式系统的复杂性,使您的应用程序能够扩展到数百台服务器.低延迟 ...
- BZOJ.1013.[JSOI2008]球形空间产生器(高斯消元)
题目链接 HDU3571 //824kb 40ms //HDU3571弱化版 跟那个一比这个太水了,练模板吧. //列出$n+1$个二次方程后两两相减,就都是一次方程了. #include <c ...
- Western Subregional of NEERC, Minsk, Wednesday, November 4, 2015 Problem I. Alien Rectangles 数学
Problem I. Alien Rectangles 题目连接: http://opentrains.snarknews.info/~ejudge/team.cgi?SID=c75360ed7f2c ...
- Mac下配置Apache服务器
有的时候,我们需要在内网工作组中分享一些文件或是后台接口没有及时给出,你又想要模拟真实数据,直接在项目里创建plist也可以做到这种需求,但难免让工程变得冗余且看起来比较Low.这个时候就看出配置本地 ...
- 马士兵hadoop第三课:java开发hdfs
马士兵hadoop第一课:虚拟机搭建和安装hadoop及启动 马士兵hadoop第二课:hdfs集群集中管理和hadoop文件操作 马士兵hadoop第三课:java开发hdfs 马士兵hadoop第 ...
- j.u.c系列(10)---之并发工具类:Semaphore
写在前面 Semaphore是一个计数信号量,它的本质是一个"共享锁". 信号量维护了一个信号量许可集.线程可以通过调用acquire()来获取信号量的许可:当信号量中有可用的许可 ...
- spy++使用指南
很多朋友都对窗口句柄比较迷糊,这篇短文就以spy++这个软件为主,介绍下窗体句柄和使用按键插件时,如果对这个句柄发送消息,即所谓的后台挂机.spy++这个软件来自VC++,装好VC后,就可以在工具中看 ...
- 百度地图api---实现新建地图
调用这个函数 function refresh() { history.go(0); } 实现了地图新建
- [Winform]setupfactory制作安装包卸载输入密码进行验证
摘要 项目有这样一个需求,在体验机上安装了一个软件,如果有用户卸载的时候,给与输入密码验证的提示,当然强制删除软件所在目录除外.那么这个有办法实现吗? 解决办法 在卸载的时候,用户单击下一步的时候进行 ...
- [Node.js]操作redis
摘要 在实际开发中,免不了要操作mysql,mongodb,redis等数据存储服务器.这里先简单介绍如何操作redis. 一个例子 关于redis服务端的安装这里不再介绍,重点不在这里.感兴趣的可以 ...