二分查找

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(二分查找)的更多相关文章

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

  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 搜索一个二维矩阵

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

  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】74. Search a 2D Matrix 解题报告(Python & C++)

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

随机推荐

  1. How the Intelligent Digital Mesh Will Transform Every Business Layer

    The "intelligent digital mesh" is the definitive package for tomorrow's titans of business ...

  2. JS插入新的节点

    insertBefore() 语法: insertBefore(newchild,refchild) newchild 插入新的节点 refchild 在此节点前插入新节点 <ul id=&qu ...

  3. Python 爬虫:把廖雪峰教程转换成 PDF 电子书

    写爬虫似乎没有比用 Python 更合适了,Python 社区提供的爬虫工具多得让你眼花缭乱,各种拿来就可以直接用的 library 分分钟就可以写出一个爬虫出来,今天尝试写一个爬虫,将廖雪峰老师的 ...

  4. sqoop1.4.6从mysql导入hdfs\hive\hbase实例

    //验证sqoop是否连接到mysql数据库sqoop list-tables --connect 'jdbc:mysql://n1/guizhou_test?useUnicode=true& ...

  5. Matplotlib常用示例入门

    一.Matplotlib介绍 Matplotlib 是一个 Python 的 2D绘图库,它以各种硬拷贝格式和跨平台的交互式环境生成出版质量级别的图形.通过 Matplotlib,开发者可以仅需要几行 ...

  6. 关于在 IntellIj IDEA中JSP页面 cannot resolve method getParameter("")的解决方案

    File->Project Structure->Libraries,然后点加号,将Tomcat lib文件夹下的servlet.jar和servlet-api.jar包导入.

  7. Android 开发笔记___DatePicker__日期选择器

    虽然EditText提供了inputTtype="date",但用户往往不太喜欢自己输入时间. Android为这个提供了DatePicker,但有很多缺点,不是弹窗模式,而是直接 ...

  8. weiapi 获取项目根目录

    无法使用: Server.Map("~"); Server.Map("~/"); Server.Map("./"); Server.Map( ...

  9. 我眼中的WebViewJavascriptBridge

    周六阳光明媚的早晨,非常适合整理和分享一些以前玩过的东西.曾经的工作中参与过一段时间iOS开发,在开发中有个小小的框架让我非常深刻,就是WebViewJavascriptBridge,用于原生控件与前 ...

  10. 【Tesseract】Tesseract 的训练流程

    在泰迪杯A题中,我刚刚接触了Tesseact,其中训练字库中遇到了较多的问题.所以在此记录一下,也当做一个笔记,省得以后忘记. 为了方便 ,将tif命名格式设为[lang].[fontname].ex ...