Search Insert Position(二分查找)
Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.
You may assume no duplicates in the array.
Here are few examples.[1,3,5,6], 5 → 2[1,3,5,6], 2 → 1[1,3,5,6], 7 → 4[1,3,5,6], 0 → 0
1.二分查找时间复杂度O(logn)。
二分查找的基本思想是将n个元素分成大致相等的两部分,去a[n/2]与x做比较,如果x=a[n/2],则找到x,算法中止;如果x<a[n/2],则只要在数组a的左半部分继续搜索x,如果x>a[n/2],则只要在数组a的右半部搜索x.
时间复杂度无非就是while循环的次数!
总共有n个元素,
渐渐跟下去就是n,n/2,n/4,....n/2^k,其中k就是循环的次数
由于你n/2^k取整后>=1
即令n/2^k=1
可得k=log2n,(是以2为底,n的对数)
所以时间复杂度为O(logn)
代码:
class Solution {
public:
int searchInsert(int A[], int n, int target) {
int l=;
int r=n-;
int mid;
while (l<=r)
{
mid=(l+r)/;
if(A[mid]>target) r=mid-;
else if(A[mid]<target) l=mid+;
else return mid;
}
if(l==r+)
return l;
else if(r=-)
return ;
else if(l=n)
return n;
}
};
2.一般方法,复杂度O(n),先找出边界,然后就是相等和两数之间的情况。
class Solution {
public:
int searchInsert(int A[], int n, int target) {
if(target>A[n-]) return n;
if(target<A[]) return ;
for(int i=;i<n;++i)
{
if(A[i]==target) return i;
if(i<(n-)&&target>A[i]&&target<A[i+])
return i+;
}
}
};
Search Insert Position(二分查找)的更多相关文章
- Leetcode 35 Search Insert Position 二分查找(二分下标)
基础题之一,是混迹于各种难题的基础,有时会在小公司的大题见到,但更多的是见于选择题... 题意:在一个有序数列中,要插入数target,找出插入的位置. 楼主在这里更新了<二分查找综述>第 ...
- LeetCode Search Insert Position (二分查找)
题意 Given a sorted array and a target value, return the index if the target is found. If not, return ...
- 35. Search Insert Position(二分查找)
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
- 【LeetCode每天一题】Search Insert Position(搜索查找位置)
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
- LeetCode 35 Search Insert Position(查找插入位置)
题目链接: https://leetcode.com/problems/search-insert-position/?tab=Description 在给定的有序数组中插入一个目标数字,求出插入 ...
- 【LeetCode】- Search Insert Position(查找插入的位置)
[ 问题: ] Given a sorted array and a target value, return the index if the target is found. If not, re ...
- LeetCode OJ:Search Insert Position(查找插入位置)
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
- LeetCode:Search Insert Position,Search for a Range (二分查找,lower_bound,upper_bound)
Search Insert Position Given a sorted array and a target value, return the index if the target is fo ...
- Leetcode 二分查找 Search Insert Position
本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Search Insert Position Total Accepted: 14279 T ...
- [Leetcode][Python]35: Search Insert Position
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 35: Search Insert Positionhttps://oj.le ...
随机推荐
- actuator服务实战
1. actuator服务实战 1.1. 前言 actuator默认集成了很多端点查看,这里我会挑选也用到可能性大些的 1.2. Endpoints 1.2.1. 使用方式 开启服务后,直接访问:lo ...
- STM32 (基础语法)笔记
指针遍历: char *myitoa(int value, char *string, int radix){ int i, d; int flag = 0; char *ptr = string; ...
- 使用Glide加载Android图片
一.概述 Glide是一个在Android端非常好的图片缓冲工具,总体上来说,他有以下优点 使用简单 自适应程度高 支持常见的图片格式,如jpg,png等 支持多种数据源,网络,本地,资源,Asset ...
- bat批处理如何删除本地策略里的用户权限分配中的拒绝从网络访问本机项的guest用户?
echo [Version]>mm.inf echo signature="$CHICAGO$">>mm.inf echo Revision=1>>m ...
- 数据结构算法 - ConcurrentHashMap 源码解析
五个线程同时往 HashMap 中 put 数据会发生什么? ConcurrentHashMap 是怎么保证线程安全的? 在分析 HashMap 源码时还遗留这两个问题,这次我们站在 Java 多线程 ...
- nodejs的学习
nodejs 就是使用js来编写服务端的程序.它的特性是(单线程 速度快 耗内存多 异步 事件驱动) ( 一些技术的解决方案:默认情况下是 1.不支持多核,可以使用cluster 进行解 ...
- day23-2 __call__、__str__和__del__
目录 __call__ __str__ __del__ __call__ 对象后面加括号调用时,会自动触发执行 注:构造方法的执行是由创建对象触发的,即:对象=类名();而对于__call__方法的执 ...
- MySQL 中去重 distinct 用法
在使用MySQL时,有时需要查询出某个字段不重复的记录,这时可以使用mysql提供的distinct这个关键字来过滤重复的记录,但是实际中我们往往用distinct来返回不重复字段的条数(count( ...
- 第2节 mapreduce深入学习:11、maptask运行机制(多看几遍)
mapTask运行机制详解以及mapTask的并行度在mapTask当中,一个文件的切片大小使用默认值是128M,就是跟我们一个block块对应大小一样 MapTask运行的整个过程 背下来1.Tex ...
- 洛谷——P1602 Sramoc问题
P1602 Sramoc问题 $bfs$搜索 保证第一个搜到的符合条件的就是最小的 #include<bits/stdc++.h> #define N 110000 using names ...