[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 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 解题思路的更多相关文章
- [LeetCode] 74 Search a 2D Matrix(二分查找)
二分查找 1.二分查找的时间复杂度分析: 二分查找每次排除掉一半不合适的值,所以对于n个元素的情况来说: 一次二分剩下:n/2 两次:n/4 m次:n/(2^m) 最坏情况是排除到最后一个值之后得到结 ...
- leetcode 74. Search a 2D Matrix 、240. Search a 2D Matrix II
74. Search a 2D Matrix 整个二维数组是有序排列的,可以把这个想象成一个有序的一维数组,然后用二分找中间值就好了. 这个时候需要将全部的长度转换为相应的坐标,/col获得x坐标,% ...
- 【LeetCode】74. Search a 2D Matrix 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 左下或者右上开始查找 顺序查找 库函数 日期 题目地 ...
- [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 ...
- 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 ...
- 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 ...
- 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 ...
- [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 ...
- 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 ...
随机推荐
- OpenCV学习目录(持续更新)
这个暑假开始,需要用到图像处理相关的东西,于是我选择了OpenCV库,这里记录下我的整个学习过程. 参考资料: <OpenCV 2计算机视觉编程手册> 张静 译,科学出版社 1. Linu ...
- 一个关于hightcharts的x轴刻度问题
最近做公司的一个报表系统,需要折线图,圆柱图形等来进行统计,经过最后考察,决定用当下较为流行的Highchart插件来进行实现,highchart用起来非常方便,只用对后台传过来的数据进行简单的处理后 ...
- C++ 11 笔记 (一) : lambda
时至今日都是我咎由自取,错就是错,与任何人无关.掉进C++98的各种坑里无法自拔的抖M感,让我选择了华丽丽的无视C++11,导致今日面对开源的代码到各种看不懂的地步,一入C++深似海,我今天愿意承担一 ...
- 如何使用Github仓库创建网站
官方文档:https://help.github.com/categories/github-pages-basics/ 1.创建一个仓库 2.额外建立一个gh-pages分支 3.添加CNAME文件 ...
- Android 使用SharedPreference来进行软件配置的存取
我们在安卓开发的时候不免需要记录用户键入的一些信息,比如账号和密码,用户使用软件的次数,上次打开软件的时间等等,为了保存这些配置,我们可以使用SharedPreference类保存他们. //使用Sh ...
- 【HDU 5370】 Tree Maker(卡特兰数+dp)
Tree Maker Problem Description Tree Lover loves trees crazily. One day he invents an interesting gam ...
- QQ协议的TEA加解密算法
QQ通讯协议里的加解密算法. #include <stdio.h> #include <stdlib.h> #include <memory.h> #include ...
- 心愿:做一个精简版MFC
我觉得自己有能力看懂MFC的C++代码和总体流程,但是由于MFC混杂了太多的东西,比如OLE等等不必要的东西,又做了无数的ASSERT判断,影响整体流程的理解.因此我要做一个精简版的MFC,而且能用它 ...
- ruby hashtable散列表
dict={'cat'=>'abc','dog'=>'def'}puts dict.size dict.keys返回所有的key, values返回所有的value. 删除: dict.d ...
- 《ruby编程语言》笔记2 对象
ruby是一门非常纯粹的面向对象的语言:所有值都是对象,而且没有基本类型(primitive type)和对象类型的区别,这一点不同于其他语言.在Ruby中,所有对象都继承一个Object类,而且共享 ...