题目:

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

思路:

二分查找。其中特别注意的是:middle=(low+high)/ 2 ,可能会溢出,可以替换为middle = low + Math.ceil((high - low)/2)

/**
* @param {number[]} nums
* @param {number} target
* @return {number}
*/
var searchInsert = function(nums, target) {
var l=0,r=nums.length-1,middle;
while(l<=r){
middle=l+Math.floor((r-l)/2);
if(nums[middle]>target){
r=middle-1;
}else if(nums[middle]<target){
l=middle+1;
}else{
return middle;
}
}
return l;
};

【数组】Search Insert Position的更多相关文章

  1. Leetcode35 Search Insert Position 解题思路(python)

    本人编程小白,如果有写的不对.或者能更完善的地方请个位批评指正! 这个是leetcode的第35题,这道题的tag是数组,python里面叫list,需要用到二分搜索法 35. Search Inse ...

  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

    本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Search Insert Position Total Accepted: 14279 T ...

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

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

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

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

  6. [LC]35题 Search Insert Position (搜索插入位置)

    ①英文题目 Given a sorted array and a target value, return the index if the target is found. If not, retu ...

  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][Python]35: Search Insert Position

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

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

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

  10. 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导致的溢 ...

随机推荐

  1. 重大发现 springmvc Controller 高级接收参数用法

    1.  数组接收 @RequestMapping(value="deleteRole.json") @ResponseBody public Object deleteRole(S ...

  2. c# richTextBox判断是否为图片文件

    //图片 if (richText.Rtf.IndexOf(@"{\pict\") > -1)//条件成立为图片(richText为一个richTextBox的实例名称)

  3. jbpm(流程管理)

    1.jbpm是什么 JBPM,全称是Java Business Process Management(业务流程管理),它是覆盖了业务流程管理.工作流.服务协作等领域的一个开源的.灵活的.易扩展的可执行 ...

  4. Delphi Dll 动态调用例子(2)

    http://zhidao.baidu.com/question/157196792.html delphi动态调用DLL 写了个1.dll内容如下 library Project2; uses Sy ...

  5. SQL server经验分享:SQLSERVER 被标记为“可疑”的数据库处理方法

    --MyDB为修复的数据名USE MASTER GO SP_CONFIGURE 'ALLOW UPDATES',1 RECONFIGURE WITH OVERRIDE GO ALTER DATABAS ...

  6. oauth入门

    oauth可以支持跨网站的数据传输.假设一个用户把照片上传到faji网站,然后想登录到beppa网站(照片打印),把faji的上照片打印出来. 她当然可以自己把照片取下来再上传上去,不过比较麻烦. 使 ...

  7. 关于CI/CD/CD (Continuous Integration/Continuous Delivery/Continuous Deployment)

    Continuous Integration (CI) Continuous integration (CI) is the process that ensures the stability of ...

  8. Storm的Metric接口简介

    本文由作者林洋港授权网易云社区发布. 作为服务端程序,我们总是需要向外界报告一些统计数据,以助于了解系统的运行情况,比如某个接口的调用时间.系统处理的请求数等等.当我们的程序以Storm Topolo ...

  9. 【Selenium专题】元素定位之一简单定位

    UI自动化工具千变万化.架构千变万化,但都逃离不开的关键一步就是元素定位.下面以Selenium为例介绍常见的几个元素定位方法 ID -元素id属性 WebElement El = driver.fi ...

  10. 《Python黑帽子:黑客与渗透测试编程之道》 基于GitHub的命令和控制

    GitHub账号设置: 这部分按书上来敲命令即可,当然首先要注册一个GitHub账号还有之前安装的GitHub API库(pip install github3.py),这里就只列一下命令吧: mkd ...