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. 整理打印PI值

    准备锻炼背诵PI的小数,找到PI值: PI=3. 141592653589793238462643383279502884197169399375105820974944592307816406286 ...

  2. java struts2入门学习--基于xml文件的声明式验证

    一.知识点总结 后台验证有两种实现方式: 1 手工验证顺序:validateXxx(针对Action中某个业务方法验证)--> validate(针对Action中所有的业务方法验证) 2 声明 ...

  3. 【LeetCode】234. Palindrome Linked List (2 solutions)

    Palindrome Linked List Given a singly linked list, determine if it is a palindrome. Follow up:Could ...

  4. 进阶之路(基础篇) - 022 Arduino Leonardo 中文介绍(摘抄)

    本文摘抄:http://www.arduino.cn/thread-1205-1-1.html 概述Arduino Leonardo是基于ATmega32u4一个微控制器板.它有20个数字输入/输出引 ...

  5. magento 为用户注册增加一个字段(转)

    步骤 I. 加一个occupation/title字段到用户注册页,差不多在register.html的54行,在email下方加一个Occupation显示代码 代码: <li>< ...

  6. go interface 的坑

    一.概述 [root@node175 demo]# tree . ├── lib │   └── world.go ├── README └── server.go directory, files ...

  7. 【Redis】解析Redis和Java传递数据

    在Java中使用Redis之前需要导入 jedis.jar 包,由于Redis是基于key-value进行数据存储,java中的数据存储到Redis中有许多方式,这里笔者介绍采用JSON字符串和对象序 ...

  8. dbms_monitor开启/关闭会话跟踪

    从10g开始,可以使用dbms_monitor开启/关闭会话跟踪. sql> desc dbms_monitor procedure client_id_stat_disable argumen ...

  9. Linux C 编程一站式学习

    个人认为这是一个挺不错的从C语言到Linux系统开发的教程,这本是两个网上的文档. 其中 一本是<How To Think Like A Computer Scientist: Learning ...

  10. 移动硬盘做pe启动盘