二分查找

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. 为Android设备添加A2SD支持

          相信很多用Android设备的用户都有这个问题,内部存储太小导致应用只能装那么几个,虽然rom也有提供移动到sd卡的选项,但是仅仅是移动程序文件到sd卡,并不能解决多少问题,多装几个还是会 ...

  2. LINUX 笔记-命令执行顺序 && ,||

    && 格式:命令1 && 命令2 说明:命令1返回真(即返回0,成功被执行)后,命令2才能够被执行 例:/apps/bin目录将会被移到/apps/dev/bin目录下 ...

  3. canvas绘制太阳系

    原文地址:http://jeffzhong.space/2017/10/26/solar/ 学习canvas有一段时间了,顺便写个小项目练手,该项目用到的知识点包括: ES6面向对象 基本的三角函数 ...

  4. phalcon——调度控制器

    将侦听者绑定到组件上: use Phalcon\Mvc\Dispatcher as MvcDispatcher, Phalcon\Events\Manager as EventsManager; $d ...

  5. C语言编写一个简单游戏

    感悟:这算是一个起点吧,我都大二了,还这么菜,才开始写游戏,这个游戏很简单,利用随机数猜大小! #include <stdlib.h> #include <stdio.h> # ...

  6. Android开发中的OpenCV霍夫直线检测(Imgproc.HoughLines()&Imgproc.HoughLinesP())

    本文为作者原创,转载请注明出处(http://www.cnblogs.com/mar-q/)by 负赑屃   //2017-04-21更新: 很多网友希望能得到源码,由于在公司做的,所以不太方便传出来 ...

  7. web前端工程师全套教程免费分享

    这是我自己早前听课时整理的前端全套知识点,适用于初学者,也可以适用于中级的程序员,你们可以下载下来.我自认为还是比较系统全面的,可以抵得上市场上90%的学习资料.讨厌那些随便乱写的资料还有拿出来卖钱的 ...

  8. centos 源码安装python

    一.准备环境 首先在官网下载想要的python对应版本http//www.python.org/downloads/source 下载tgz就可以了.文件有两种 1,Python-版本号.tgz(解压 ...

  9. html-webpack-plugin的使用

    使用前第一步:npm install 安装html-webpack-plugin --save--dev || --save  (tips:--save--dev跟--save最大的区别就是--dev ...

  10. 机器翻译评测——BLEU改进后的NIST算法

    ◆版权声明:本文出自胖喵~的博客,转载必须注明出处. 转载请注明出处:http://www.cnblogs.com/by-dream/p/7765345.html 上一节介绍了BLEU算的缺陷.NIS ...