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

Hide Tags

Array Binary Search

 

  这题其实便是二分搜索,需要考虑没有找到的时候的情况。
#include <iostream>
using namespace std; class Solution {
public:
int searchInsert(int A[], int n, int target) {
if(n==) return ;
// if(target<=A[0]) return 0;
if(A[n-]<target) return n;
int l=,r=n-,m=;
while(l<=r){
m=(l+r)/;
// cout<<l<<" "<<m<<" "<<r<<endl;
if(A[m]==target) return m;
if(A[m]<target) l=m+;
else r=m-;
}
if(target<A[m]) return m;
return m+;
}
}; int main()
{
int a[]={,};
Solution sol;
for(int i=;i<=;i++)
cout<<i<<" "<<sol.searchInsert(a,sizeof(a)/sizeof(int),i)<<endl;
return ;
}

[LeetCode] Search Insert Position 二分搜索的更多相关文章

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

  2. LeetCode: Search Insert Position 解题报告

    Search Insert Position Given a sorted array and a target value, return the index if the target is fo ...

  3. [LeetCode] Search Insert Position 搜索插入位置

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

  4. [LeetCode] Search Insert Position

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

  5. leetcode Search Insert Position Python

    #Given a sorted array and a target value, return the index if the target is found. If #not, return t ...

  6. LeetCode Search Insert Position (二分查找)

    题意 Given a sorted array and a target value, return the index if the target is found. If not, return ...

  7. LeetCode——Search Insert Position

    Description: Given a sorted array and a target value, return the index if the target is found. If no ...

  8. [Leetcode] search insert position 寻找插入位置

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

  9. LeetCode Search Insert Position (二分查找)

    题意: 给一个升序的数组,如果target在里面存在了,返回其下标,若不存在,返回其插入后的下标. 思路: 来一个简单的二分查找就行了,注意边界. class Solution { public: i ...

随机推荐

  1. opengl 学习的链接,以后需要可以再来查需要的

    记录一些好的opengl学习站点,以供日后查阅: modern opengl tutorial : 一个英国的opengl学习网站 上面网站的中文版 日后发现新的再更新

  2. 第七篇:suds.TypeNotFound: Type not found: '(string, http://schemas.xmlsoap.org/soap/encoding/, )'

    想要用Python的suds模块调用webservice地址做自动测试,但是找了很多方法都失败了,最终找到另外一个模块可以作为客户端访问服务器地址. 1.针对非安全的http from zeep im ...

  3. nginx安装php环境

    1.php下载地址 https://secure.php.net/downloads.php(此次安装版本为7.0.33) 2.安装依赖的包 yum -y install libxml2 yum -y ...

  4. ThinkPHP函数I代码优化

    ThinkPHP/Common/common.php 文件 I函数,主要用来获取一些gpc请求的变量的,函数有一部分代码是过滤变量的,每次都运行一次,其实是没有必要的. 如果你每次都像这样的方式调用的 ...

  5. spark入门: wordcount-java

    wordcount-java: pom.xml文件如下: <dependencies> <dependency> <groupId>junit</groupI ...

  6. g++编译器的使用(转载)

    关于g++ g++  是GNU组织开发出的编译器软件集合(GCC)下的一个C++编译器.它是Unix 和 Linux  系统下标配的 基于命令行的 C++编译器.如果你的系统是Windows,可以按照 ...

  7. docker 学习(3)

    docker和宿主之间的数据共享以及docker间的数据共享仍然是让人头疼和操心的地方. 几个基本概念: docker: 一种容器管理技术,这里也指既有的开发工具链. container: 容器 im ...

  8. http--一次完整的HTTP事务是怎样一个过程?【转】

    一次完整的HTTP事务是怎样一个过程? 如有收获请给作者点赞 --> 原文链接 声明:本文章中的说法仅是个人理解总结,不一定完全正确,但是可以有助于理解. 当我们在浏览器的地址栏输入 www.l ...

  9. easyui的tree基本属性

    1.cascadeCheck,级联 默认情况下,是true,级联的,就是选中一个子节点,父节点是半选中状态,子节点全选中之后,父节点就是选中状态.   

  10. Python学习之正则表达式初探

    正则表达式 正则表达式 (或 regexes ) 是通用的文本模式匹配的方法. Django URLconfs 允许你 使用任意的正则表达式来做强有力的URL映射,不过通常你实际上可能只需要使用很少的 ...