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. linux command screen

    喜欢这句话 “我们永远相信,分享是一种美德 | We Believe, Great People Share Knowledge...” 但是这种美德不是为什么都要问的人准备的 ——————————— ...

  2. 随机采样方法整理与讲解(MCMC、Gibbs Sampling等)

    本文是对参考资料中多篇关于sampling的内容进行总结+搬运,方便以后自己翻阅.其实参考资料中的资料写的比我好,大家可以看一下!好东西多分享!PRML的第11章也是sampling,有时间后面写到P ...

  3. transform scale 背景图片模糊怎么办?

    transform: translateZ(0) scale(1, 1); 就是这样(摊手表情),不晓得什么原理.

  4. app安装位置声明

    AndroidManifest.xml <manifest xmlns:android="http://schemas.android.com/apk/res/android" ...

  5. Centos6.7安装docker1.7.1

    Docker当前发布的最新版本已经到了1.11,其官网上针对Centos的的安装需求如下: Docker requires a -bit installation regardless of your ...

  6. YTU 2345: 后序遍历二叉树

    原文链接:https://www.dreamwings.cn/ytu2345/2611.html 2345: 后序遍历二叉树 时间限制: 1 Sec  内存限制: 128 MB 提交: 3  解决:  ...

  7. [CCF] Z字形扫描

    CCF Z字形扫描 感觉和LeetCode中的ZigZag还是有一些不一样的. 题目描述 在图像编码的算法中,需要将一个给定的方形矩阵进行Z字形扫描(Zigzag Scan).给定一个n×n的矩阵,Z ...

  8. FreeSWITCH中文语音包

    一.中文语音资源的获取 官方提供的资源:http://files.freeswitch.org/releases/sounds/ 自己录音 实在不行可以@我给你发一份. 二.中文资源的安装 英文资源的 ...

  9. iOS开发数据库篇—SQLite常用的函数

    iOS开发数据库篇—SQLite常用的函数 一.简单说明 1.打开数据库 int sqlite3_open( const char *filename,   // 数据库的文件路径 sqlite3 * ...

  10. JPA入门案例详解(附源码)

    1.新建JavaEE Persistence项目