[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/ 目录 题目描述 题目大意 解题方法 左下或者右上开始查找 顺序查找 库函数 日期 题目地 ...
随机推荐
- GoldenGate 复制进程报错"OGG-01296 Error mapping",丢弃文件报错“Mapping problem with delete record (target format)”,且实际条目存在
故障描述: (1).复制进程 Abended,通过view report语句查看可发现类似如下的报错: 2017-10-23 15:01:43 ERROR OGG-01296 Error mappin ...
- git 一口气带你走完git之旅
1.git是目前世界上最先进的分布式版本控制系统.svn是集成式版本控制系统,那么问题来了,什么叫分布式管理和集中式管理? 首先,svn 需要有一个中央服务器,协同开发者需要同中央服务器连接,所有的版 ...
- Promise对象解读
首先强调的是"Promise"是对象,也就是说与其他JavaScript对象的用法,没有什么两样:其次,它起到代理作用(proxy),充当异步操作与回调函数之间的中介.它使得异步操 ...
- clone对象
在JavaScript中,当对象作为参数传给函数的时候,在函数内部对这个对象的属性进行修改时,函数外部的对象属性也会跟着被修改,而有些时候我们并不想原来的对象数据发生改变,这时候就需要切断对象之间的引 ...
- LeetCode 80. Remove Duplicates from Sorted Array II (从有序序列里移除重复项之二)
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...
- LeetCode 79. Word Search(单词搜索)
Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from l ...
- Ceph: A Scalable, High-Performance Distributed File System译文
原文地址:陈晓csdn博客 http://blog.csdn.net/juvxiao/article/details/39495037 论文概况 论文名称:Ceph: A Scalable, High ...
- LAMP 实现全过程及wordpress的搭建
一.介绍 1. LAM(M)P: L:linux A:apache (httpd) M:mysql, mariadb M:memcached 缓存 P:php, perl, python WEB 资源 ...
- Leetcode题解(32)
107. Binary Tree Level Order Traversal II 题目 直接代码: /** * Definition for a binary tree node. * struct ...
- Java简单实现UDP和TCP
TCP实现 TCP协议需要在双方之间建立连接,通过输入输出流来进行数据的交换,建立需要通过三次握手,断开需要四次挥手,保证了数据的完整性,但传输效率也会相应的降低. 简单的TCP实现 //服务端 pu ...