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

这题一开始想用从左到右,再上到下的方法去实现,但是不行,后来看了别人的实现,应该使用的是先取右上的元素与target进行比较,如果大于target的话那么这一列都不会存在target,小于的话那么改行都不会存在该元素,从而进行下一步的搜寻,代码如下:

 class Solution {
public:
bool searchMatrix(vector<vector<int>>& matrix, int target) {
if(!matrix.size() || !matrix[].size())
return false;
int rowCount = matrix.size();
int colCount = matrix[].size();
for(int i = , j = colCount - ; i < rowCount && j >= ; ){
if(matrix[i][j] == target) return true;
else if(matrix[i][j] > target)
j--;
else
i++;
}
return false;
}
};

LeetCode OJ:Search a 2D Matrix II(搜寻二维矩阵)的更多相关文章

  1. [LeetCode] 74. Search a 2D Matrix 搜索一个二维矩阵

    Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...

  2. LeetCode 74. 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. 240 Search a 2D Matrix II 搜索二维矩阵 II

    编写一个高效的算法来搜索 m x n 矩阵中的一个目标值.该矩阵具有以下特性:    每行的元素从左到右升序排列.    每列的元素从上到下升序排列.例如,考虑下面的矩阵:[  [1,   4,  7 ...

  4. Leetcode240. Search a 2D Matrix II搜索二维矩阵2

    编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target.该矩阵具有以下特性: 每行的元素从左到右升序排列. 每列的元素从上到下升序排列. 示例: 现有矩阵 matrix ...

  5. [CareerCup] 11.6 Search a 2D Matrix 搜索一个二维矩阵

    11.6 Given an M x N matrix in which each row and each column is sorted in ascending order, write a m ...

  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] 240. Search a 2D Matrix II 搜索一个二维矩阵 II

    Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...

  8. (medium)LeetCode 240.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 ...

  9. Leetcode 240 Search a 2D Matrix II (二分法和分治法解决有序二维数组查找)

    1.问题描写叙述 写一个高效的算法.从一个m×n的整数矩阵中查找出给定的值,矩阵具有例如以下特点: 每一行从左到右递增. 每一列从上到下递增. 2. 方法与思路 2.1 二分查找法 依据矩阵的特征非常 ...

  10. Leetcode 240. 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 ...

随机推荐

  1. 论文笔记:IRGAN——A Minimax Game for Unifying Generative and Discriminative Information

    2017 SIGIR 简单介绍 IRGAN将GAN用在信息检索(Information Retrieval)领域,通过GAN的思想将生成检索模型和判别检索模型统一起来,对于生成器采用了基于策略梯度的强 ...

  2. XPO开发指南简要

    一.XPO简介: XPO即eXpress Persistent Objects for .NET,现在这里介绍的版本是1.5. XPO在应用程序代码和数据库之间扮演了一个中间层的角色,简单而言,就是将 ...

  3. 20145240《网络对抗》MSF基础应用

    MSF基础应用 一个主动攻击,如ms08_067 启动msf search ms08_067,查找相应的漏洞,查询可攻击的模块. 根据上述漏洞的模块use exploit/windows/smb/ms ...

  4. Could not reserve enough space for 1572864KB object heap

    This problem might be caused by incorrect configuration of the daemon.For example, an unrecognized j ...

  5. Shell学习小结 - 深入认识变量

    移动端访问不佳,请访问我的个人博客 变量的命名 对于初学者来说,可以简单的理解为,变量就是保存在计算机内存中的一系列的键值对. 列如: str="hello" 这里的str就是变量 ...

  6. composer安装教程 windows系统 | Linux系统 | mac系统

    如何安装 Composer 下载 Composer 安装前请务必确保已经正确安装了 PHP.打开命令行窗口并执行 php -v 查看是否正确输出版本号. 打开命令行并依次执行下列命令安装最新版本的 C ...

  7. mysql分库分表(二)

    mysql分库分表 参考: https://www.cnblogs.com/dongruiha/p/6727783.html https://www.cnblogs.com/oldUncle/p/64 ...

  8. Hadoop 常用指令

    1. 察看hdfs文件系统运行情况 bin/hdfs dfsadmin -report 2. 为了方便执行 HDFS 的操作指令,我们可以将需要的 Hadoop 路径写入环境变量中,便于直接执行命令. ...

  9. Java循环语句之 do...while

    do...while 循环与 while 循环语法有些类似,但执行过程差别比较大. 语法: 执行过程: <1>. 先执行一遍循环操作,然后判断循环条件是否成立 <2>. 如果条 ...

  10. css tips —— 在css中完成国际化

    前提 在日常处理国际化的时候,通常是将key通过类似intl.xx(key)转换为对应环境的文案,可是如果需要在css中加入对应逻辑应该怎么做呢(比如在after的伪元素中显示不同的文案),毕竟在cs ...