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

解题思路一:

二分查找,注意边界条件,JAVA实现如下:

	static public int searchInsert(int[] nums, int target) {
int left = 0, right = nums.length - 1;
while (left <= right) {
if (target == nums[(left + right) / 2])
return (left + right) / 2;
else if (target < nums[(left + right) / 2]) {
if ((left + right) / 2 - 1 < left)
return target > nums[left] ? left + 1 : left;
if (target > nums[(left + right) / 2 - 1])
return (left + right) / 2;
if (target == nums[(left + right) / 2 - 1])
return (left + right) / 2 - 1;
right = (left + right) / 2 - 1; } else {
if ((left + right) / 2 + 1 > right)
return target > nums[right] ? right + 1 : right;
if (target <= nums[(left + right) / 2 + 1])
return (left + right) / 2 + 1;
left = (left + right) / 2 + 1;
}
}
return right;
}

解题思路二:

同样采用二分,可以通过某些方法,减少代码量,JAVA实现如下:

static public int searchInsert(int[] nums, int target) {
int left = 0, right = nums.length - 1;
while (left < right) {
if (nums[(right + left) / 2] < target)
left = (right + left) / 2 + 1;
else
right = (right + left) / 2;
}
return nums[left] >= target ? left : left + 1;
}

Java for LeetCode 035 Search Insert Position的更多相关文章

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

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

  2. LeetCode 035 Search Insert Position

    题目要求:Search Insert Position Given a sorted array and a target value, return the index if the target ...

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

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

  4. [LeetCode] 35. Search Insert Position 搜索插入位置

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

  5. Java [leetcode 35]Search Insert Position

    题目描述: Given a sorted array and a target value, return the index if the target is found. If not, retu ...

  6. LeetCode 35. 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 35 Search Insert Position(查找插入位置)

    题目链接: https://leetcode.com/problems/search-insert-position/?tab=Description   在给定的有序数组中插入一个目标数字,求出插入 ...

  8. 035 Search Insert Position 搜索插入位置

    给定一个排序数组和一个目标值,如果在数组中找到目标值则返回索引.如果没有,返回到它将会被按顺序插入的位置.你可以假设在数组中无重复元素.案例 1:输入: [1,3,5,6], 5输出: 2案例 2:输 ...

  9. [leetcode 35] Search Insert Position

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

随机推荐

  1. BZOJ-1951 古代猪文 (组合数取模Lucas+中国剩余定理+拓展欧几里得+快速幂)

    数论神题了吧算是 1951: [Sdoi2010]古代猪文 Time Limit: 1 Sec Memory Limit: 64 MB Submit: 1573 Solved: 650 [Submit ...

  2. Bzoj3893 [Usaco2014 Dec]Cow Jog

    Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 302  Solved: 157 Description The cows are out exerci ...

  3. UVa 1025 A Spy in the Metro

    http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=35913 预处理出每个时间.每个车站是否有火车 为了方便判断是否可行,倒推处理 ...

  4. POJ1753Flip Game(DFS + 枚举)

    Flip Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 37050   Accepted: 16122 Descr ...

  5. Android事件机制之一:事件传递和消费

    http://www.cnblogs.com/lwbqqyumidi/p/3500997.html 关于Android中的事件机制,用到的地方还是很多的,并且这个知识点还真有点复杂. 在写这篇文章前, ...

  6. Laravel教程 四:数据库和Eloquent

    Laravel教程 四:数据库和Eloquent 此文章为原创文章,未经同意,禁止转载. Eloquent Database 上一篇写了一些Laravel Blade的基本用法和给视图传递变量的几种方 ...

  7. dedecms网站栏目增加缩略图的方法-测试通过

    有时候因为网站功能需求,我们需要为织梦程序的栏目页添加缩略图功能,这里有一个栏目添加缩略图的方法,供大家参考 涉及到文件如下(注意备份): dede/catalog_add.php dede/cata ...

  8. app 后端技术

    app 后端技术 一直以来工作的方向是web server,对app server没有什么了解.虽然没有接触过移动app开发,但对app后端技术还是挺有探索欲望的,app应用和web应用在前端的用户习 ...

  9. Visual Studio Online Integrations-Productivity

                                        原文:http://www.visualstudio.com/zh-cn/explore/vso-integrations-di ...

  10. Log4j使用教程 log4:WARN No appenders could be found for logger (org.springframework.web.context.ContextLoader).

    1.Logger类 通过Logger类的静态方法Logger.getRootLogger得到RootLogger.所有其他的loggers是通过静态方法Logger.getLogger来实例化并获取的 ...