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

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

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.

矩阵排列规则。同一行从左往右递增,同一列从上往下递增,从右上角的数字下手

AC代码

class Solution {public:    bool searchMatrix(vector
>& matrix, int target) { int rows=matrix.size(); if(rows==0) return false; int colums=matrix[0].size(); int x=colums-1; int y=0; while(y
=0) { if(matrix[y][x]==target) return true; else if(matrix[y][x]>target) --x; else ++y; } return false; }};
其它Leetcode题目AC代码:

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

你可能感兴趣的文章
Mybatis知识(1)
查看>>
[CentOS] 7 不执行文件 /etc/rc.d/rc.local
查看>>
模态窗口的各个属性
查看>>
10.28 (上午) 开课一个月零二十四天 (数据访问)
查看>>
为什么你应该(从现在开始就)写博客
查看>>
小技巧积累
查看>>
Java JDBC链接Oracle数据库
查看>>
Moss2010 部署命令
查看>>
Git 操作分支
查看>>
Grid search in the tidyverse
查看>>
hdu 三部曲 Contestants Division
查看>>
day22——创建表、增加数据、查询数据
查看>>
css伪元素实现tootip提示框
查看>>
关于函数指针的总结
查看>>
采用PHP函数uniqid生成一个唯一的ID
查看>>
Centos7安装32位库用来安装32位软件程序
查看>>
【HMOI】小C的填数游戏 DP+线段树维护
查看>>
java中23种设计模式之6-适配器模式(adapter pattern)
查看>>
Easy C 编程 in Linux
查看>>
poj3761(反序表)
查看>>