博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[LeetCode] Search a 2D Matrix II 搜索一个二维矩阵之二
阅读量:6895 次
发布时间:2019-06-27

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

hot3.png

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.

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

 

突然发现LeetCode很喜欢从LintCode上盗题,这是逼我去刷LintCode的节奏么?! 这道题让我们在一个二维数组中快速的搜索的一个数字,这个二维数组各行各列都是按递增顺序排列的,是之前那道的延伸,那道题的不同在于每行的第一个数字比上一行的最后一个数字大,是一个整体蛇形递增的数组。所以那道题可以将二维数组展开成一个一位数组用一次二查搜索。而这道题没法那么做,这道题有它自己的特点。如果我们观察题目中给的那个例子,我们可以发现有两个位置的数字很有特点,左下角和右上角的数。左下角的18,往上所有的数变小,往右所有数增加,那么我们就可以和目标数相比较,如果目标数大,就往右搜,如果目标数小,就往上搜。这样就可以判断目标数是否存在。当然我们也可以把起始数放在右上角,往左和下搜,停止条件设置正确就行。代码如下:

class Solution {public:    bool searchMatrix(vector
> &matrix, int target) { if (matrix.empty() || matrix[0].empty()) return false; if (target < matrix[0][0] || target > matrix.back().back()) return false; int x = matrix.size() - 1, y = 0; while (true) { if (matrix[x][y] > target) --x; else if (matrix[x][y] < target) ++y; else return true; if (x < 0 || y >= matrix[0].size()) break; } return false; }};

转载于:https://my.oschina.net/u/2935389/blog/3038364

你可能感兴趣的文章
python matplotlib及sklearn安装
查看>>
困惑2017?
查看>>
KOTree
查看>>
BlockAlertsAnd-ActionSheets
查看>>
开源 java CMS - FreeCMS2.5 标签formTable自定义表单
查看>>
FreeCMS视频教程 将FreeCMS导入myeclipse
查看>>
Android 8.0 SystemUI(一):图文并茂的介绍 :D
查看>>
1wifi 简介(框架)
查看>>
internet && intranet
查看>>
go get报错 error: RPC failed; result=56, HTTP code =
查看>>
串行(Sequential)、并发(Concurrent)、并行(parallel)与分布式
查看>>
JAVA NIO学习笔记之Channel(基础篇)
查看>>
Xcode升级到6.4之后插件无法使用,重新安装最新也无法使用的解决办法
查看>>
秒懂科技新概念
查看>>
eclipse启动tomcat无法访问
查看>>
Notepad++ 书签
查看>>
TiDB 集群测试
查看>>
十天学会php之第五天
查看>>
Java基础10
查看>>
jquery基础学习二
查看>>