博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[leetcode]Search a 2D Matrix @ Python
阅读量:7066 次
发布时间:2019-06-28

本文共 845 字,大约阅读时间需要 2 分钟。

原题地址: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

 

转载地址:http://krtll.baihongyu.com/

你可能感兴趣的文章
蒙哥玛利模幂算法
查看>>
龙威零式_团队项目例会记录_14
查看>>
YAFFS2文件系统分析(转)
查看>>
获取GET/POST提交的数据,并处理中文问题
查看>>
jdbc 获取connection 对象的三种方式
查看>>
jsp标签+jstl
查看>>
第二阶段个人总结09
查看>>
FATAL ERROR: Could not find ./bin/my_print_defaults的解决办法
查看>>
文摘《十一》
查看>>
jquery 笔记。。。——》摘自武方博
查看>>
一个夭折,
查看>>
C#开发微信门户及应用(1)--开始使用微信接口(转)
查看>>
Kali-linux使用社会工程学工具包(SET)
查看>>
ScriptManager(脚本控制器)
查看>>
Android chromium 2
查看>>
poj_3468,线段树成段更新
查看>>
什么是mybatis?
查看>>
【算法导论】学习笔记——第6章 堆排序
查看>>
NS3编译运行
查看>>
Python+Appium自动化环境搭建
查看>>