[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/ 目录 题目描述 题目大意 解题方法 左下或者右上开始查找 顺序查找 库函数 日期 题目地 ...
随机推荐
- OOAD-设计模式(二)之GRASP模式与GOF设计模式概述
一.GRASP模式(通用责任分配软件模式)概述 1.1.理解责任 1)什么是责任 责任是类间的一种合约或义务,也可以理解成一个业务功能,包括行为.数据.对象的创建等 知道责任——表示知道什么 行为责任 ...
- thinkphp3.2开发网页实现第三方登录
1.在要添加登录的html里添加登录按钮: <a href="{:U('Public/login/',array('type'=>'weixin'))}">< ...
- Java调用C++类库--JNI
JNI是Java平台中的一个重要的功能,这里我把我做的Demo总结一下,分享一下,我会把每个步骤尽量的详细的展现出来. 这里我就不讲解JNI的原理了,google,百度一下,到处都是 好了,直接来讲步 ...
- NumPy基础练习(练一遍搞定NumPy)
import numpy as np import pandas as pd from numpy import random from numpy.random import randn ##### ...
- MySQL-client-5.6.36-1.linux_glibc2.5.x86_64.rpm安装详解
centos6.8已经安装了mysql,所以要卸载掉 查看命令 rpm -qa | grep mysql 注意:MySQL区分大小写 grep mysql 和grep MySQL 是不一样的!! 卸载 ...
- 1.Nginx 简介
Nginx是Apache服务器不错的替代品,它能支持高达50 000个并发连接数的响应,而内存,CPU等系统资源消耗却非常低,运行非常稳定. 1.选择Nginx的理由 1.1 可以高并发连接 1.2 ...
- Node.js Web 模块
什么是 Web 服务器? Web服务器一般指网站服务器,是指驻留于因特网上某种类型计算机的程序,Web服务器的基本功能就是提供Web信息浏览服务.它只需支持HTTP协议.HTML文档格式及URL,与客 ...
- javascript中原型链与instanceof 原理
instanceof:用来判断实例是否是属于某个对象,这个判断依据是什么呢? 首先,了解一下javascript中的原型继承的基础知识: javascript中的对象都有一个__proto__属性,这 ...
- XMLHttpRequest2 异步 ajax
XMLHttpRequest1只是对已经存在的xhr对象细节进行规范定义, XMLHttpRequest2升级了该对象. FormData 类型可以用在xhr传输的时候,把表单序列化或者将数据以表 ...
- Wordpress上传文件 “无法建立目录wp-content/uploads/2018/25。有没有上级目录的写权限?”
可能的原因有两个: wp-content/uploads 文件夹权限所限制 解决方法:修改wp-content/uploads 文件夹权限,可以借助ftp等工具: wp_options表中upload ...