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.

问题:在给定的已排序的二维矩阵中,判断是否包含某个整数。

思路:看到二维数组,首先想到的是二维数组和 一维数组可以直接转换,arr[i][j] 等于 arr[i*col + j]。而在一个已排序的一维数组中搜索一个元素,可以采用分治(Divide and Conquer)思想,更具体些就是二分搜索(Binary Search) 算法。

将上面的结合起来就是,先实现一维数组的二分搜索算法,然后将算法中的一位转换为二维数组即可。

 bool searchMatrix(vector<vector<int>>& matrix, int target) {

     if (matrix.size() == ) {
return false;
} int col = (int)matrix[].size();
int row = (int)matrix.size(); if (col == && row == ) {
return ( matrix[][] == target );
} int lm = ;
int rm = col * row - ; while (lm < rm ) { if (lm + == rm) {
int quoL = lm / col;
int remL = lm % col; int quoR = rm / col;
int remR = rm % col; if (matrix[quoL][remL] == target || matrix[quoR][remR] == target) {
return true;
}else{
return false;
}
} int midm = (lm + rm) / ; int quo = midm / col;
int rem = midm % col; if (matrix[quo][rem] == target) {
return true;
} if (matrix[quo][rem] < target) {
lm = midm;
}else{
rm = midm;
}
} return false;
}

[LeetCode] 74. Search a 2D Matrix 解题思路的更多相关文章

  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 解题报告(Python & C++)

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

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

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

    Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...

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

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

随机推荐

  1. Nginx中让 重写后的路径 自动增加斜线 /

    http://www.111cn.net/sys/nginx/56067.htm(参考文章) 现在有个这样的需求,在重写的url地址后,自动加斜线 / 例如 xx.com/abc/1-2  (默认ur ...

  2. js hover放大效果

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  3. input多选图片与显示

    input标签 在使用input选择文件时遇到了 在遍历input file.files 只显示最后一个,修改如下: CSS: <style type="text/css"& ...

  4. PHP PhantomJs中文文档(翻译)

    介绍 PHP PhantomJS 是一个灵活的 PHP 库加载页面通过 PhantomJS 无头浏览器并将返回页面响应.这是方便于需要JavaScript的支持,同时还支持截屏测试网站.功能列表通过 ...

  5. Linux命令 &与&&的作用

    1.ls &表示后台服务 2.ls && ll 表示前者执行成功,执行后台命令

  6. C++ static全局变量与全局变量的区别/static全局函数与全局函数的区别

    全局变量(外部变量)的说明之前再冠以static 就构成了静态的全局变量.全局变量本身就是静态存储方式, 静态全局变量当然也是静态存储方式.这两者在存储方式上并无不同.这两者的区别虽在于非静态全局变量 ...

  7. source insight添加汇编文件.s

    (1) 做ARM嵌入式开发时,有时得整汇编代码,但在SIS里建立PROJECT并ADD TREE的时候,根据默认设置并不会把该TREE里面所有汇编文件都包含进来,默认只加了.inc和.asm后缀的, ...

  8. 工作流(worfflow)

    -- 工作流(Workflow)就是“业务过程的部分或整体在计算机应用环境下的自动化”,它主要解决的是“使在多个参与者之间按照某种预定义的规则传递文档.信息或任务的过程自动进行,从而实现某个预期的业务 ...

  9. Pentaho Data Integration (三) Pan

    官网连接: http://wiki.pentaho.com/display/EAI/Pan+User+Documentation Pan Pan 是一个可以执行使用Spoon编辑的transforma ...

  10. EXCEL : We can't do that to a merged cell.