Search Insert Position

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

SOLUTION 1:

用九章算法的模板查找结束后判断一下即可。:

 public int searchInsert1(int[] A, int target) {
if (A == null || A.length == 0) {
return 0;
} int left = 0;
int right = A.length - 1; while (left < right - 1) {
int mid = left + (right - left) / 2;
int num = A[mid]; if (num == target) {
return mid;
} else if (num < target) {
left = mid + 1;
} else {
right = mid - 1;
}
} // bug 1: should use <=
if (target <= A[left]) {
return left;
// bug 2: should use <= . consider that may the result exit in left or right.
} else if (target <= A[right]) {
return right;
} return right + 1;
}

SOLUTION 2:

也可以很简洁:

http://fisherlei.blogspot.com/2013/01/leetcode-search-insert-position.html

http://blog.csdn.net/fightforyourdream/article/details/14216321

这样可以word的原因是:

1. 当target存在,当然返回mid.

2. 当target大于所有的数。则l, r会跑到最右边,并且l会继续跑出去一格,也就是l会指向 len,也就是要找的值。

3. 当target小于所有的数。l,r跑到最左边,并且r会继续往左移动一格,l指向目标位置。

4. 当target小于某数a,并大于某数b。那么l, r中止时,r会在b,l 会在a,l 指向目标位置。

若是找不到target, 循环结束后l 的值是 与target最接近但是 > target 的数在数组中的位置。

 // sol 2:
public int searchInsert(int[] A, int target) {
if (A == null || A.length == 0) {
return 0;
} int left = 0;
int right = A.length - 1; while (left <= right) {
int mid = left + (right - left) / 2;
int num = A[mid]; if (num == target) {
return mid;
} else if (num < target) {
left = mid + 1;
} else {
right = mid - 1;
}
} return left;
}

GTIHUB:

https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/divide2/SearchInsert.java

LeetCode: Search Insert Position 解题报告的更多相关文章

  1. 【LeetCode】35. Search Insert Position 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 二分查找 日期 题目地址:https://leetc ...

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

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

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

  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 二分搜索

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

  6. [LeetCode] Search Insert Position

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

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

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

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

  9. LeetCode——Search Insert Position

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

随机推荐

  1. 实测iOS Dynamic Framework 对 App 启动时间的影响效果

    最近看到的Slow App Startup Times里提到: The dynamic loader finds and reads the dependent dynamic libraries ( ...

  2. syslog远程日志存储/514端口【转】

    昨天在抓包的时候,发现在514端口,有SYSLOG字段的东西,不知道是用来干啥的,现在来分析一下: 其实他是在电脑间用了syslog远程日志存储,他用udp监控了514端口的数据流,之后收集整理日志: ...

  3. 【MySQL】MySQL之MySQL5.7安装包(msi文件)在Windows8下安装

    最近自己在使用MySQL5.7.16.msi安装MySQL.自己下载的是.msi文件,在安装的过程中遇到了许多文件,网上大部分的Blog都是关于免安装包的安装方法,希望我的方法对大家有帮助. 1,下载 ...

  4. .Net jsc.exe 编译js 成exe

  5. [Aaronyang] 写给自己的WPF4.5 笔记5[数据绑定三巴掌1/3]

    生活总有意外,微笑对待每一件事,无需抱怨--Aaronyang的博客(www.ayjs.net) 博文摘要:数据库下载 教你如何在vs2013中不安装Mssql数据库,使用了Sqlserver Com ...

  6. Android基础之——CountDownTimer类,轻松实现倒计时功能

    在发现这个类之前,一直是用的handler,子线程发消息,UI线程进行倒计时的显示工作.前几天在做一个倒计时显示的时候发现了这个类,用起来非常方便 翻看了下源代码.内部已经帮我们实现了handler的 ...

  7. iOS runtime执行时具体解释

    什么是runtime? runtime直译就是执行时间,run(跑,执行) time(时间),网上大家都叫它执行时,它是一套比較底层的纯C语言API,属于一个C语言库,包括了非常多底层的C语言API, ...

  8. 浅谈Mysql 表设计规范(转)

    本文首先探讨下数据库设计的三大范式,因为范式只是给出了数据库设计的原则,并没有告诉我们实际操作中应该怎样操作,应该注意什么,所以我们还会谈下实际工作中需要注意的具体操作问题. 三大范式 首先放出三大范 ...

  9. 为什么推荐你用 Kotlin语言?

    谷歌大牛说:为什么 Kotlin 比你们用的那些垃圾语言都好 原标题:谷歌大牛说:为什么 Kotlin 比你们用的那些垃圾语言都好 编译:伯乐在线/黄小非 [伯乐在线/程序员的那些事 导读]:5月18 ...

  10. 解决iPad/iPhone下手机号码会自动被加上a标签的问题

    解决办法: 在页面顶部<head></head>中增加: <meta name="format-detection" content="te ...