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. linux anaconda 管理 python 包

    1.下载 anaconda https://www.continuum.io/downloads 2.安装anaconda 3.conda install package-name //利用anaco ...

  2. python中的json的基本使用方法

    在python中使用json的时候,主要也就是使用json模块,json是以一种良好的格式来进行数据的交互,从而在很多时候,可以使用json数据格式作为程序之间的接口, #!/usr/bin/env ...

  3. sysbench基准测试(2)——oltp.lua测试

    前面知道sysbench基准测试的主要步骤为:prepare(准备数据集)→ run(运行测试)→ cleanup(清除数据集) 这一节介绍oltp.lua测试. oltp基准测试模拟了一个简单的事物 ...

  4. Console2支援中文顯示的正式設定法

    1.用regedit找到HKEY_CURRENT_USER\Console,把底下的Console2 command window機碼給砍了.2.Console2的View功能表中,有個Console ...

  5. Linux运维跳槽必备的40道面试精华题

    过一次年,结婚.存款.父母养老,一系列向钱看的事都在碾压我们本来还挺简单的神经,但难过没有出路,唯有找到好的方法和事业方向,才能实现一步一个脚印的逆袭. 下面是一名资深Linux运维求职数十家公司总结 ...

  6. <转>Linux环境进程间通信(五): 共享内存(下)

    http://www.ibm.com/developerworks/cn/linux/l-ipc/part5/index2.html 系统调用mmap()通过映射一个普通文件实现共享内存.系统V则是通 ...

  7. eclipse Oxygen 4.7 + pydev

    pydev 官网  安装手册 PyDev requires Java 8 and Eclipse 4.6 (Neon) in order to run and only supports Python ...

  8. 并发编程和MySQL总结

    1️⃣  并发编程主要内容: 操作系统工作原理介绍.线程.进程演化史.特点.区别.互斥锁.信号.事件.join.GIL.进程间通信.管道.队列. 生产者消费者模型.异步模型.IO多路复用模型.sele ...

  9. 如何debug?(转载)

    本文属于转载,原文地址如下: https://blog.csdn.net/zhao4zhong1/article/details/53078924 一.码畜:靠编译器帮自己查语法错误 消灭笔误:编写适 ...

  10. 275. H-Index II 递增排序后的论文引用量

    [抄题]: Given an array of citations in ascending order (each citation is a non-negative integer) of a ...