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. 呕心沥血Android studio使用JNI实例

    发现网上很多JNI的使用教程,也很详细,不过有的地方有些缺漏,导致很多小问题难以解决的,今天就来总结一下. 准备工作:下载NDK. 简单的说,要用到C/C++,就要用NDK.直接百度搜索然后去官网下载 ...

  2. DotNetBar For Windows Forms 12.5.0.2 官方原版及注册

    转自原文DotNetBar For Windows Forms 12.5.0.2 官方原版及注册 DotNetBar是一款带有56个 Windows Form 控件的工具箱,使开发人员可以轻而易举地创 ...

  3. proxmox 安装ROS 备忘

    虚拟机设置:使用qemu64 CPU和vrtio网卡在家里测试性能最好.

  4. 产品负责人(Product Owner)的主要职责和技能

    角色介绍 产品负责人以下简称PO,他是有授权的产品领导力核心,组成Scrum团队三个角色之一. PO担任的是产品经理的角色. PO的主要职责 1.对产品的ROI负责. ROI = profitabil ...

  5. Delphi数据库的三层架构的问题和解决方法

    Delphi数据库的三层架构的问题和解决方法 原创 2014年03月26日 16:26:03 标签: Delphi / 数据库三层架构 / DCOM / DCOMConnection 790 //-- ...

  6. Elasticsearch from+size 超过10000结果解决方法

    方法一: 如果需要搜索分页,可以通过from size组合来进行.from表示从第几行开始,size表示查询多少条文档.from默认为0,size默认为10, 如果搜索size大于10000,需要设置 ...

  7. bootstrap-select.js 下拉框多选后动态赋值

    话不多说先上demo 其实demo是从官网下载的 只稍作改动 由于没有搞清楚怎么上传源代码 就把官网的链接贴出来吧 https://github.com/silviomoreto/bootstrap- ...

  8. Hadoop Yarn Capacity Scheduler

    Capacity 调度器配置 <property> <name>yarn.resourcemanager.scheduler.class<name> <val ...

  9. apt 查询软件

    apt-cache search percona-server apt list percona-server-server-5.6

  10. Openssl pkeyutl命令

    一.简介 pkeyutl命令能够测试所支持的密钥算法的性能 二.语法 openssl rsautl [-in file] [-out file] [-sigfile file] [-inkey fil ...