[LeetCode] 74 Search a 2D Matrix(二分查找)
二分查找
1.二分查找的时间复杂度分析:
二分查找每次排除掉一半不合适的值,所以对于n个元素的情况来说:
一次二分剩下:n/2
两次:n/4
m次:n/(2^m)
最坏情况是排除到最后一个值之后得到结果,所以:
n/(2^m) = 1
2^m = n
所以时间复杂度为:log2(n)
2.二分查找的实现方法:
(1)递归
int RecursiveBinSearch(int arr[], int bottom, int top, int key) {
if (bottom <= top) {
int mid = (bottom + top) / 2;
if (arr[mid] == key) {
return mid;
}
else if (key < arr[mid]) {
return RecursiveBinSearch(arr, bottom, mid - 1, key);
}
else {
return RecursiveBinSearch(arr, mid + 1, top, key);
}
}
else {
return -1;
}
}
(2)非递归
int nonRecursiveBinSearch(int arr[], int size, int key) {
int bottom = 0, top = size - 1, mid;
while (bottom <= top) {
mid = (bottom + top) / 2;
if (arr[mid] == key) {
return mid;
}
else if (key < arr[mid]) {
top = mid - 1;
}
else {
bottom = mid + 1;
}
}
return -1;
}
3.LeetCode题目:74 Search a 2D Matrix
原题地址:
https://leetcode.com/problems/search-a-2d-matrix/description/
题目:

解法:
这道题给出一个二维数组(已排序),再给定一个数,让我们确定这个数是否在这个二维数组里面。由于这个二维数组是排好序的,因此我们可以使用两次二分查找,第一次使用先定位好这个数在第几行,第二次使用确定这个数在第几列。
代码如下:
class Solution {
public:
bool searchMatrix(vector<vector<int>>& matrix, int target) {
if (matrix.size() == || matrix[].size() == ) return false;
int col = getCol(matrix, , matrix.size() - , target);
if (col == -) {
return false;
}
else {
return isExist(matrix, col, , matrix[col].size() - , target);
}
}
int getCol(vector<vector<int>>& matrix, int first, int last, int target) {
if (first > last) return -;
int mid = (first + last) / ;
if (matrix[mid][] <= target && target <= matrix[mid][matrix[mid].size() - ]) {
return mid;
}
else {
if (target < matrix[mid][]) {
return getCol(matrix, first, mid - , target);
}
else {
return getCol(matrix, mid + , last, target);
}
}
}
bool isExist(vector<vector<int>>& matrix, int col, int first, int last, int target) {
if (first > last) {
return false;
}
else {
int mid = (first + last) / ;
if (matrix[col][mid] == target) {
return true;
}
else {
if (matrix[col][mid] > target) {
return isExist(matrix, col, first, mid - , target);
}
else {
return isExist(matrix, col, mid + , last, target);
}
}
}
}
};
后来出于好奇直接用两层循环来查找,最后所花的时间竟然和用二分查找一样,哎:
class Solution {
public:
bool searchMatrix(vector<vector<int>>& matrix, int target) {
for (int i = ; i < matrix.size(); i++) {
for (int j = ; j < matrix[i].size(); j++) {
if (matrix[i][j] == target) return true;
}
}
return false;
}
};
[LeetCode] 74 Search a 2D Matrix(二分查找)的更多相关文章
- 74. Search a 2D Matrix(二分查找,剑指offer 1)
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 、240. Search a 2D Matrix II
74. Search a 2D Matrix 整个二维数组是有序排列的,可以把这个想象成一个有序的一维数组,然后用二分找中间值就好了. 这个时候需要将全部的长度转换为相应的坐标,/col获得x坐标,% ...
- [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 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】74. Search a 2D Matrix 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 左下或者右上开始查找 顺序查找 库函数 日期 题目地 ...
随机推荐
- 【ASP.NET MVC 学习笔记】- 14 HtmlHlper的扩展方法
本文参考:http://www.cnblogs.com/willick/p/3428413.html 1.在 MVC 中用于生成 Html 元素的辅助类是 System.Web.Mvc 命名空间下的 ...
- toString方法的用处
p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 15.0px Consolas; color: #a5b2b9 } p.p1 { margin: 0.0px ...
- vue.js快速搭建图书管理平台
前 言 上一期简单讲解了vue的基本语法,这一次我们做一个小项目,搭建一个简单的图书管理平台,能够让我们更深刻的理解这门语言的妙用. 1.DEMO样式 首先我们需要搭建一个简单的demo样式 ...
- 五分钟上手Markdown
Markdown是一种可以使用普通文本编辑器编写的标记语言,通过简单的标记语法,它可以使普通文本内容具有一定的格式.------百度百科 话不多说,开始发车!总共10个标签,五分钟足矣,毕竟基本没难度 ...
- java的集合框架set 和map的深入理解
Java的集合框架之Map的用法详解 Map有两种比较常用的实现:HashMap 和 TreeMap. HashMap: HashMap 也是无序的,也是按照哈希编码来排序的,允许使用null 值和n ...
- oracle赋值问题(将同一表中某一字段赋值给另外一个字段的语句)
将同一表中某一字段赋值给另外一个字段的语句update jxc_ckmx ckmx1 set ckmx1.ddsl = (select ckmx2.sl from jxc_ckmx ckmx2 whe ...
- CSS基础:基础和语法
**CSS语法** CSS 规则由两个主要的部分构成:选择器,以及一条或多条声明.选择器通常是您需要改变样式的 HTML 元素.```selector {declaration1; declarati ...
- 如何在Pypi上发表自己的Python库
背景 最近兴趣使然写了几个Python库,也发布到了Pypi上,虽然没什么人下载,但自己在其他机器上用着也会很方便.这里我向大家介绍一下如何在Pypi上发表自己的Python库. 准备 注册账号 很显 ...
- Windows下caffe的配置和调用caffe库(二)
二. Caffe库的调用: 新建空白项目,将配置管理器更改为x64运行方式.(release和Debug均可). Debug配置: 1) 包含目录: D:\caffe-master\incl ...
- Android模仿iOS iMessages10照片选择器的实现
不知不觉已经接近半年多没有写过博客了,这段时间,也是我刚好毕业走出校园的时间,由于学习工作的原因,一直没有真正静下心来写下些什么东西.这个星期刚入了小米笔记本pro的坑,本着新电脑新生活的理念嘻嘻-- ...