题目

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. Java 中类与类之间的关系

    在java中类和类之间的关系基本上有依赖.关联.聚合.组合等 一.继承关系     继承指的是一个类(称为子类.子接口)继承另外的一个类(称为父类.父接口)的功能,并可以增加它自己的新功能的能力.在J ...

  2. 转载:10 Easy Steps to a Complete Understanding of SQL

    10 Easy Steps to a Complete Understanding of SQL 原文地址:http://tech.pro/tutorial/1555/10-easy-steps-to ...

  3. ejs简单教程

    ejs learning nodejs的模板引擎有很多, ejs是比较简单和容易上手的.常用的一些语法: 用<%...%>包含js代码 用<%=...%>输出变量 变量若包含 ...

  4. Android技术宅:自制USB OTG数据线

    作为一名Android技术宅,USB OTG是你必须了解的,所谓USB OTG就是你可以利用手机或平板上用来充电.与电脑传输数据的micro USB接口来连接其他USB外设,如游戏手柄.鼠标.键盘.U ...

  5. mongodb与mysql相比的优缺点

    与关系型数据库相比,MongoDB的优点:①弱一致性(最终一致),更能保证用户的访问速度:举例来说,在传统的关系型数据库中,一个COUNT类型的操作会锁定数据集,这样可以保证得到“当前”情况下的精确值 ...

  6. bootstrap注意事项(二)

    1.内联子标题 在标题内还可以包含 <small> 标签或赋予 .small 类的元素,可以用来标记副标题. <!DOCTYPE html> <html> < ...

  7. ecshop二次开发之购物车常见问题

    1.ecshop二次开发中保存注册用户购物车数据解决方法:ecshop购物车是数据库中cart表来支持的,在ecshop表中rec_id是编号,user_id是注册用户的id,session_id表示 ...

  8. 《OS X Mountain Lion》 读书杂记

    OS X是一个类UNIX操作系统,由底层的Darwin和上层的OS X应用程序框架(Cocoa, Carbon, Quartz等)及Aqua用户界面组成.其中Darwin是一个开源.完整的POSIX- ...

  9. InterviewProblems

    package com.xiaoysec; /** * 下面是面试趣医网技术面的时候出现的一个简单的题目 题目的要求是将一个数组中的奇数和偶数进行分离 以奇数在前一部分为例进行解题 * 算法的主要思想 ...

  10. C语言统计运行时间

    clock()是C/C++中的计时函数,而与其相关的数据类型是clock_t. 在MSDN中,查得对clock函数定义如下: clock_t clock(void) ;   简单而言,就是该程序从启动 ...