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 start = , end = matrix.size()-;
int mid;
int lineNum;
while(start<=end){
mid = start + ((end-start)>>);
if(matrix[mid][]<target){
start = mid+;
}
else if(matrix[mid][]>target){
end = mid-;
}
else return true;
}
if(end < ) return false;
lineNum = end;
start = ;
end = matrix[].size()-;
while(start<=end){
mid = start + ((end-start)>>); if(matrix[lineNum][mid]<target){
start = mid+;
}
else if(matrix[lineNum][mid]>target){
end = mid-; }
else return true;
}
return false;
}
};

74. Search a 2D Matrix (Graph; Divide-and-Conquer)的更多相关文章

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

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

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

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

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

  4. 【LeetCode】74. Search a 2D Matrix 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 左下或者右上开始查找 顺序查找 库函数 日期 题目地 ...

  5. 74. Search a 2D Matrix

    题目: Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the f ...

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

  7. 【LeetCode】74. Search a 2D Matrix

    Difficulty:medium  More:[目录]LeetCode Java实现 Description Write an efficient algorithm that searches f ...

  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. flv格式的播放代码

    <object classid="clsid:D27CDB6E-AE6D-444553540000" class="player2" codebase=& ...

  2. fullfile

    这个我总是忽略,见过也不少了,顺便写写,其实一些命令很方便的. 一个例子: root_dir = '../mcg/pre-trained'; addpath(root_dir); addpath(fu ...

  3. BZOJ2140: 稳定婚姻(tarjan解决稳定婚姻问题)

    2140: 稳定婚姻 Time Limit: 2 Sec  Memory Limit: 259 MBSubmit: 1321  Solved: 652[Submit][Status][Discuss] ...

  4. Visual Studio2010 支持MVC4开发

    最近的项目有一个维护的版本使用的是Visual Studio2010+MVC4开发的,记录一下软件的开发环境 ============================================= ...

  5. 《DSP using MATLAB》示例 Example 10.2

    代码: %% ------------------------------------------------------------------------ %% Output Info about ...

  6. linux下nginx安装、配置实战

    1什么是Nginx Nginx("enginex")是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP代理服务器,在高连接并发的情况下Nginx是Apac ...

  7. gridview 绑定多个格式相同的数据源(数据查询合并)

    在做项目时,要求在同一个GridView中同时显示不同分组中的前若干条数据 几个技术要点: 1.数据分组,本方法中未用group by 2.几个结果格式相同的查询合并绑定 3.取查询结果的前或后若干条 ...

  8. 桶排序与快速排序算法结合-python实现

    #-*- coding: UTF-8 -*- import numpy as np from QuickSort import QuickSort def BucketSort(a, n): barr ...

  9. linux系统报错日志学习

    linux本身会自动记录系统报错日志:/var/log/messages 这个日志记录,我是在什么时候发现其强大的作用的呢?它有点像我们使用php脚本开发接口的时候技术员在重要地方打日志的效果,方便技 ...

  10. .NET实现WebSocket服务端即时通信实例

    即时通信常用手段 1.第三方平台 谷歌.腾讯 环信等多如牛毛,其中谷歌即时通信是免费的,但免费就是免费的并不好用.其他的一些第三方一般收费的,使用要则限流(1s/限制x条消息)要么则限制用户数. 但稳 ...