题目

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.

原题链接(点我)

解题思路

二维数组中的查找,这是个简单的题,依据题意能够推出,这个二维数组事实上是一个有序的一维数组。解决思路也非常easy想到,每次比較每一维最后一个元素,假设该元素比要找的元素小,说明这个行不可能含该元素;假设相等,那就找到了,假设最后一个元素比要找元素大,说明该元素假设出现比在这一行。然后再在这一行中即可查找(能够用顺序,也可二分)。

代码实现

class Solution {
public:
bool searchMatrix(vector<vector<int> > &matrix, int target) {
int m = matrix.size();
if(m<=0) return false;
int n = matrix[0].size();
for(int i=0; i<m; ++i){
if(matrix[i][n-1] == target)
return true;
else if(matrix[i][n-1] < target)
continue;
else{
for(int j=0; j<n; ++j)
if(matrix[i][j] == target)
return true;
return false;
}
}
return false;
}
};
假设你认为本篇对你有收获,请帮顶。

另外,我开通了微信公众号--分享技术之美,我会不定期的分享一些我学习的东西.
你能够搜索公众号:swalge 或者扫描下方二维码关注我

(转载文章请注明出处: http://blog.csdn.net/swagle/article/details/29564491
)

[LeetCode] Search a 2D Matrix [25]的更多相关文章

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

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

  3. LeetCode Search a 2D Matrix II

    原题链接在这里:https://leetcode.com/problems/search-a-2d-matrix-ii/ Write an efficient algorithm that searc ...

  4. LeetCode: Search a 2D Matrix 解题报告

    Search a 2D Matrix Write an efficient algorithm that searches for a value in an m x n matrix. This m ...

  5. LeetCode -- Search a 2D Matrix & Search a 2D Matrix II

    Question: Search a 2D Matrix Write an efficient algorithm that searches for a value in an m x n matr ...

  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]Search a 2D Matrix @ Python

    原题地址:https://oj.leetcode.com/problems/search-a-2d-matrix/ 题意: Write an efficient algorithm that sear ...

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

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

随机推荐

  1. QueryString和BASE64

    加号(+)是BASE64编码的一部分,而加号在QueryString中被当成是空格.因此,当一个含有BASE64编码的字符串直接作为URL的一部分时,如果其中含有加号,则使用QueryString读取 ...

  2. 【Lucene】挖掘相关搜索词

    搜索引擎中往往有一个可选的搜索词的列表,当搜索结果太少时,可以帮助用户扩展搜索内容,或者搜索结果太多的时候可以帮助用户深入定向搜索.一种方法是从搜索日志中挖掘字面相似的词作为相关搜索词列表.另一种方法 ...

  3. Cannot retrieve metalink for repository: epel.

    Error: Cannot retrieve metalink for repository: epel. Please verify its path and                     ...

  4. /var/cache/apt/archives/lock - open

    问题: E: 无法获得锁 /var/cache/apt/archives/lock - open (11 资源临时不可用)E: 无法锁定下载目录o rm /var/cache/apt/archives ...

  5. 安装virtualbox虚拟机的增强功能

    转自:http://wubangtu.com/714 最近有很多人问我这个问题,现在全部写在这里,免得到时候又啰嗦一遍了,哈哈.欢迎大家前来围观: 安装virtualbox虚拟机的增强功能可以实现如下 ...

  6. 最简单的视音频播放演示样例4:Direct3D播放RGB(通过Texture)

    ===================================================== 最简单的视音频播放演示样例系列文章列表: 最简单的视音频播放演示样例1:总述 最简单的视音频 ...

  7. ProductHunt:创业公司产品猎场和秀场

    Product模式介绍 ProductHunt(站点www.producthunt.com)是一个国外新出现的创业公司产品展示和交流平台, 顾名思义.对于创业者而言这里是一个秀场,而对于投资人而言这里 ...

  8. 一台服务器同时搭建IIS和WAMP,利用WAMP 80端口转发

    打开wamp 里面的 httpd.conf 文件,找到以下四个语句,取消注释 #LoadModule proxy_module modules/mod_proxy.so -->LoadModul ...

  9. java打包/命令行方式运行jar(命令行进行程序测试)

    public class Testtmp { public static void main(String[] args) { // TODO Auto-generated method stub f ...

  10. 记录一个php强制下载文件函数

    整理个经常用的强制下载文件的函数,之前就说把一些常用的东西整理出来结果老是没时间,最近陆续把这些东西整理下. public function download() { $id = $this-> ...