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.

面试宝典上的经典题目,在此不赘述了,按照题目描述,找规律即可。

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

转载请注明出处:http://www.cnblogs.com/double-win/谢谢!

[LeetCode 题解]: Search a 2D Matrix的更多相关文章

  1. LeetCode 题解 Search a 2D Matrix II。巧妙!

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

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

  3. [Leetcode Week13]Search a 2D Matrix

    Search a 2D Matrix 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/search-a-2d-matrix/description/ D ...

  4. [LeetCode] 74 Search a 2D Matrix(二分查找)

    二分查找 1.二分查找的时间复杂度分析: 二分查找每次排除掉一半不合适的值,所以对于n个元素的情况来说: 一次二分剩下:n/2 两次:n/4 m次:n/(2^m) 最坏情况是排除到最后一个值之后得到结 ...

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

  6. 【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 ...

  7. leetcode 74. Search a 2D Matrix 、240. Search a 2D Matrix II

    74. Search a 2D Matrix 整个二维数组是有序排列的,可以把这个想象成一个有序的一维数组,然后用二分找中间值就好了. 这个时候需要将全部的长度转换为相应的坐标,/col获得x坐标,% ...

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

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

随机推荐

  1. [z]兼容IE6的相对于浏览器窗口定位

    http://blog.uedsc.com/css-position.html http://www.w3cmm.com/notepad/css-position.html http://sofish ...

  2. BIO模型

    基本模型 代码: 客户端 package bhz.bio; import java.io.BufferedReader; import java.io.IOException; import java ...

  3. GeoServer之图层的新建与发布

    GeoServer之图层的新建与发布 GeoServer的图层发布并不复杂,在经过: 1.创建工作区 2.添加新的数据存储 3.编写styles 后:我们就可以很简单的创建图层了. 1.在新建图层中选 ...

  4. nrm操作

    nrm操作 nrm use cnpm // 选择镜像nrm ls //查看镜像

  5. SpringMVC将表单对象序列化成Json字符串提交,以List接收

    出自:http://blog.csdn.net/m0_37595732/article/details/71440853 HTML <%@ page language="java&qu ...

  6. 好一个Time_Wait状态(TCP/IP)

    首先简单介绍一下Time_Wait是个什么鬼: 在TCP/IP协议中,我们都知道有三次握手四次挥手的过程,先来一个简单的图: 各个状态和基本的过程想必了解过TCP/IP协议的人都清楚,本次介绍的主题只 ...

  7. pip 18.1 官方文档翻译

    快速开始 从pypi上安装一个包 $ pip install SomePackage 查看安装的包里面包含什么文件 pip show --files SomePackage 查看已经安装的包里面哪些是 ...

  8. debian:jessie 安转percona mysql

    dpkg -r percona-server-server-5.6 dpkg -r percona-server-client-5.6 dpkg -r libperconaserverclient18 ...

  9. poolmanager1.path-o-logical 很好的prefab preload工具

    http://docs.poolmanager1.path-o-logical.com/ 提高性能的不错选择

  10. 572. Subtree of Another Tree 大树里包括小树

    [抄题]: Given two non-empty binary trees s and t, check whether tree t has exactly the same structure ...