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. JS Jquery去除数组重复元素

    js jquery去除数组中的重复元素 第一种:$.unique() 第二种: for(var i = 0,len = totalArray_line.length;i < len;i++) { ...

  2. The Honeynet ProjectThe Honeynet Project

    catalogue . 蜜罐基本概念 . Kippo: SSH低交互蜜罐安装.使用 . Dionaea: 低交互式蜜罐框架部署 . Thug . Amun malware honeypots . Gl ...

  3. TCP/IP详解 学习七

    静态选路的前提: 1)         网络比较小 2)         网络之间单点连接 3)         网络之间没有多余的路由 动态选路协议,用于路由器之间的通信,有以下几种: 1)     ...

  4. Laravel教程 二:路由,视图,控制器工作流程

    Laravel教程 二:路由,视图,控制器工作流程 此文章为原创文章,未经同意,禁止转载. View Controller 上一篇教程我们走了那么长的路,终于把Laravel安装好了,这一篇教程我们就 ...

  5. 删除ecshop底部共执行个查询Gzip 已禁用,占用内存方法

    删除ecshop底部共执行个查询Gzip 已禁用,占用内存方法 ECSHOP教程/ ecshop教程网(www.ecshop119.com) 2013-03-25   “共执行 41 个查询,用时 2 ...

  6. DFS & BFS

    DFS 深度优先 BFS 广度优先 DFS或者BFS都是在联通区域内遍历节点的方法 用在二叉树上DFS有preOreder,inOrder,postOrder,BFS就是层次遍历. 在二叉树上的节点, ...

  7. XSS 自动化检测 Fiddler Watcher & x5s & ccXSScan 初识

    一.标题:XSS 自动化检测 Fiddler Watcher & x5s  & ccXSScan 初识     automated XSS testing assistant 二.引言 ...

  8. CentOS 7 使用经验(更新中)

    首先说一下写这篇博客的初衷. 由于公司这一期的产品准备支持的环境有CentOS 7.MySql 5.6.Java 8.Tomcat 8等等,并且因为人员严重不足,我本月的开发任务在原有的基础上又加上了 ...

  9. Linux中iptables设置详细(转)

    无论如何,iptables是一个需要特别谨慎设置的东西,万一服务器不在你身边,而你贸然设置导致无法SSH,那就等着被老板骂吧,呵呵... 以下内容是为了防止这种情况发生而写的,当然很初级,不过一般服务 ...

  10. C#设计模式(3):抽象工厂模式(Abstract Factory)(转载)

    概述 在软件系统中,经常面临着“一系列相互依赖的对象”的创建工作:同时由于需求的变化,往往存在着更多系列对象的创建工作.如何应对这种变化?如何绕过常规的对象的创建方法(new),提供一种“封装机制”来 ...