[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 ...
随机推荐
- [翻译][MVC 5 + EF 6] 2:基础的增删改查(CRUD)
原文:Implementing Basic CRUD Functionality with the Entity Framework in ASP.NET MVC Application 1.修改Vi ...
- C++11中新特性之:initializer_list详解
C++11提供的新类型,定义在<initializer_list>头文件中. template< class T > class initializer_list; 先说它的用 ...
- mysql主从 1050错误
在mysql从库上查询时出现如下错误 ...................... Last_Errno: 1050 Last_Error: Error 'Tab ...
- ES6笔记-正则表达式和字符串正则方法
RegExp构造函数 在ES5中,RegExp构造函数的参数有两种情况. 第一种情况是,参数是字符串,这时第二个参数表示正则表达式的修饰符(flag). var regex = new RegExp( ...
- 4MLinux7.0 服务器配置详解 别名TheSSS
TheSSS download 特性:thttp,php5.5.1,mysql,vsftp,proxy,firewall,带rpm管理器.更新频繁. 官方帮助文件:View (新窗口打开) 发现国内4 ...
- 使用win8.1 x64 office2010 php 使用 pdo_odbc 连接excel失败的问题
public function init($filePath){ $dbq = iconv('UTF-8',"GBK",BASEPATH.'../'.$filePath); $ds ...
- maya2105 - windows8 - numpy/scipy
To compile numpy, create a site.cfg file in numpy's source directory with the following or similar c ...
- DataTable,DataSet,DataRow与DataView
DataTable和DataSet可以看做是数据容器,比如你查询数据库后得到一些结果,可以放到这种容器里,那你可能要问:我不用这种容器,自己读到变量或数组里也一样可以存起来啊,为什么用容器?原因是,这 ...
- 微软CSS面试全记录
先是会有一轮简单的电话技术面试,聊的比较随意,什么都会问,跟职位相关的都有.然后会发一些材料说是要学习,是windows内存管理相关的东西. 完了就是一轮oral test,和技术没有任何关系,问问为 ...
- KVO - 键值观察
[基本概念] 键值观察是一种使对象获取其他对象的特定属性变化的通知机制.控制器层的绑定技术就是严重依赖键值观察获得模型层和控制器层的变化通知的.对于不依赖控制器层类的应用程序,键值观察提供了一种简化的 ...