原题地址: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的更多相关文章

  1. [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 ...

  2. [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 ...

  3. LeetCode Search a 2D Matrix II

    原题链接在这里:https://leetcode.com/problems/search-a-2d-matrix-ii/ Write an efficient algorithm that searc ...

  4. 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 ...

  5. 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 ...

  6. 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 ...

  7. [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 ...

  8. [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 ...

  9. LeetCode Search a 2D Matrix II (技巧)

    题意: 有一个矩阵,每行有序,每列也有序.判断一个数target是否存在于此矩阵中. 思路: 从右上角开始,如果当前数字<target,则该行作废.若当前数字>target,该列作废.这样 ...

随机推荐

  1. CO文件升级

    当在Process Designer文件中导入旧版本CO模型时,拖入Graphic View后会出现如下错误.升级CO文件可解决该错误.   使用开始菜单中Tecnomatix下的Update to ...

  2. 【HDU5909】Tree Cutting(FWT)

    [HDU5909]Tree Cutting(FWT) 题面 vjudge 题目大意: 给你一棵\(n\)个节点的树,每个节点都有一个小于\(m\)的权值 定义一棵子树的权值为所有节点的异或和,问权值为 ...

  3. nginx 编译参数详解(运维必看)

    nginx参数: –prefix= 指向安装目录 –sbin-path 指向(执行)程序文件(nginx) –conf-path= 指向配置文件(nginx.conf) –error-log-path ...

  4. Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) C. Ray Tracing 数学

    C. Ray Tracing 题目连接: http://codeforces.com/contest/724/problem/C Description oThere are k sensors lo ...

  5. j.u.c系列(11)---之并发工具类:Exchanger

    写在前面 前面三篇博客分别介绍了CyclicBarrier.CountDownLatch.Semaphore,现在介绍并发工具类中的最后一个Exchange.Exchange是最简单的也是最复杂的,简 ...

  6. slf4j 和 log4j合用的(Maven)配置

    简述: 添加logger的日志输出,下面是配置信息供备忘   步骤: 1. 在Maven的porn.xml 文件中添加dependency如下 <dependency> <group ...

  7. CentOS下KVM配置NAT网络(网络地址转换模式)

    KVM虚拟机Nat方式上网: # 查看当前活跃的网络 virsh net-list # 查看该网络的详细配置 virsh net-dumpxml default 客户机的XML配置文件中interfa ...

  8. VGA Output from STM32F4 Discovery board

    VGA Output from STM32F4 Discovery board I love the web! There are so many cool projects out there, a ...

  9. cmd 递归删除目录或文件

    递归删目录 for /r <TARGET DIR> %i in (<DIR NAME or Pattern>) do rd /s /q %i 递归删文件 for /r < ...

  10. 树莓派 Windows10 IoT Core 开发教程

    入门指引 现在让我们把LED连接到安装了Windows10 IoT Core 的硬件设备,并创建一个应用程序来让它们闪烁. 在Visual Studio中加载工程 首先在这里找到例程,这里有C++和C ...