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. Vcenter server 5.5安装部署

    1.安装VMware ESXi Server 虚拟主机安装方法请看本人博客 "实践记忆": http://www.cnblogs.com/zoulongbin/p/5896836. ...

  2. UCOS 解读代码

    1.OSInit()函数:建立两个任务,一个是空闲任务,在任何任务没有就绪时运行,一个是统计任务,计算cpu的利用率.初始化 UCOSII 的所有变量和数据结构,2.OSTaskCreate 该函数返 ...

  3. objective-c第六章课后练习6

    题6:接受从终端输入的整数,提取并用英语显示这个数的每一个数字,如932,显示nine three two (题目中注了.这个练习很难)的确有点难,自己想了很久网上也各种搜索.也算是找到参考了 cod ...

  4. 弹性伸缩布局flex

    Flex 布局教程:语法篇   作者: 阮一峰 日期: 2015年7月10日 网页布局(layout)是CSS的一个重点应用. 布局的传统解决方案,基于盒状模型,依赖 display属性 + posi ...

  5. sqlyong64位破解

    姓名(Name):cr173 序列号(Code):8d8120df-a5c3-4989-8f47-5afc79c56e7c 或者(OR) 姓名(Name):cr173 序列号(Code):59adfd ...

  6. viewdata向view传递匿名类,view该如何遍历?

    //方法中的代码var userInfoList = from p in db.UserInfo select new {UserID=p.ID, UserName = p.UName, UserPw ...

  7. Python基础第二篇

    一.三元运算 if True: name='a' else: name='b' #上面的代码用三元运算表示: name="a" if True else "b" ...

  8. WebRequest使用

    // 待请求的地址 string url = "http://www.cnblogs.com"; // 创建 WebRequest 对象,WebRequest 是抽象类,定义了请求 ...

  9. [问题2014S05] 解答

    [问题2014S05] 解答  (本解答由谷嵘同学提供) 首先, 由 \(\mathrm{tr}(AB)=\mathrm{tr}(BA)\) 可得 \(a=0\), 或者由 Cauchy-Binet ...

  10. [问题2014S13] 复旦高等代数II(13级)每周一题(第十三教学周)

    [问题2014S13]  (1)  设 \(A\) 是数域 \(\mathbb{K}\) 上的 \(n\) 阶非异阵, 若存在主对角元全为 \(1\) 的下三角阵 \(L\in M_n(\mathbb ...