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

Summary: basic binary search stuff.

     int searchInsert(int A[], int n, int target) {
int start = ;
int end = n - ;
while(start <= end) {
int median = start + (end - start + ) / ;
if(target == A[median]) {
return median;
}else if(target < A[median]) {
if(median - >= && target > A[median - ])
return median;
if(median == )
return ;
end = median - ;
}else {
if(median + <= n - && target < A[median + ])
return median + ;
if(median == n - )
return n;
start = median + ;
}
}
}

Search Insert Position [LeetCode]的更多相关文章

  1. Search Insert Position——LeetCode

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

  2. Search Insert Position leetcode java

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

  3. [Leetcode][Python]35: Search Insert Position

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 35: Search Insert Positionhttps://oj.le ...

  4. [array] leetcode - 35. Search Insert Position - Easy

    leetcode - 35. Search Insert Position - Easy descrition Given a sorted array and a target value, ret ...

  5. [LeetCode] 035. Search Insert Position (Medium) (C++)

    索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 035. Sea ...

  6. Leetcode之二分法专题-35. 搜索插入位置(Search Insert Position)

    Leetcode之二分法专题-35. 搜索插入位置(Search Insert Position) 给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引.如果目标值不存在于数组中,返回它将会 ...

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

  8. leetcode 704. Binary Search 、35. Search Insert Position 、278. First Bad Version

    704. Binary Search 1.使用start+1 < end,这样保证最后剩两个数 2.mid = start + (end - start)/2,这样避免接近max-int导致的溢 ...

  9. LeetCode: Search Insert Position 解题报告

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

随机推荐

  1. tcpdump教程入门

    tcpdump是一个最基本重要的网络分析工具, 掌握好这, 对于学习tcp/ip协议也是很有帮助的. 理解了tcp/ip协议栈的知识, 分析调优网络的能力才会更高. 所以使用tcpdump相比其它的工 ...

  2. 对字符串算md5

    这个问题要是写代码 是很简单的一个问题 能不能再简单一点呢,比如一条命令  一条sql,当然你要是在线转换也很快 shell printf admin|md5sum 注意printf 与echo区别 ...

  3. 中国电信大亚DP607光猫破解,设置路由,wifi!关闭远程管理,改连接限制,SN码查询!

    破解方法:以下为破解方法(一):1.进入192.168.1.12.用户名输入anonymous,密码不用,登陆3.输入网址192.168.1.1/backupsettings.conf4.用记事本打开 ...

  4. PHP基础班初学心得:关于网页创作

    前提:本人刚参加PHP基础班培训第一天,由于之前毫无基础,分享的心得可能不规范,方法也许也"旁门左道",不能保证质量,只作自己总结学习,也希望能帮助到同样是初学者的朋友们,共同进步 ...

  5. [Effective JavaScript 笔记]第63条:当心丢弃错误

    管理异步编程的一个是错误处理.同步代码中只要使用try语句块包装一段代码很容易一下子处理所有的错误. try{ f(); g(); h(); } catch(e){ //这里用来下得出现的错误 } t ...

  6. ajax用法流程

    这里是用javascript做的一个ajax的一个用法以及总结概括.供友友们进行参考. 1 window.onload=function() { var oBtn=document.getElemen ...

  7. A feature in Netsuite Reports > Financial > Balance Sheet

    最新版本的Customize balance sheet page Left side > Layout > Add Reference Row Then in right side, y ...

  8. (转)MySQL命令行--导入导出数据库

    MySQL命令行导出数据库:   1,进入MySQL目录下的bin文件夹:cd MySQL中到bin文件夹的目录 如我输入的命令行:cd C:\Program Files\MySQL\MySQL Se ...

  9. 13.KVM安装之网桥

    安装必须的几个库和软件(最好下载一个163的yum源,速度快点) $ yum -y install kvm python-virtinst libvirt tunctl bridge-utils vi ...

  10. 【转】IP分片和TCP分段

    ZC: 由于 TCP中 MSS 的关系,TCP 不会造成 IP分片和TCP分段 ! 1.http://zhidao.baidu.com/link?url=YCnR8B-1EN4-cgauRtwa-iV ...