鱼C论坛

 找回密码
 立即注册
查看: 2065|回复: 0

[学习笔记] Leetcode 240. Search a 2D Matrix II

[复制链接]
发表于 2020-9-11 09:15:10 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
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 in ascending from left to right.
Integers in each column are sorted in ascending from top to bottom.
Example:

Consider the following matrix:

[
  [1,   4,  7, 11, 15],
  [2,   5,  8, 12, 19],
  [3,   6,  9, 16, 22],
  [10, 13, 14, 17, 24],
  [18, 21, 23, 26, 30]
]
Given target = 5, return true.

Given target = 20, return false.

  1. class Solution:
  2.     def searchMatrix(self, matrix, target):
  3.         """
  4.         :type matrix: List[List[int]]
  5.         :type target: int
  6.         :rtype: bool
  7.         """
  8.         if matrix == None or len(matrix) == 0 or len(matrix[0]) == 0:
  9.             return False
  10.         
  11.         row, col = 0, len(matrix[0]) - 1
  12.         while row < len(matrix) and col >= 0:
  13.             curt = matrix[row][col]
  14.             
  15.             if curt < target:
  16.                 row +=1
  17.             elif curt > target:
  18.                 col -= 1
  19.             else:
  20.                 return True
  21.         return False
复制代码

本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-18 08:56

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表