题目:

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

题意:

给定一个有序数组和一个目标值。假设在数组中找到该目标值。就返回这个目标值的位置标号。假设没有找到,就返回该目标值插入该数组中时的位置标号。

你能够如果该数组中没有反复元素。

以下是一些列子:

[1,3,5,6],
5 → 2

[1,3,5,6],
2 → 1

[1,3,5,6],
7 → 4

[1,3,5,6],
0 → 0

算法分析:

二分搜索就可以。

直接上代码

AC代码:

<span style="font-size:12px;">public class Solution
{
public int searchInsert(int[] A, int target)
{
int i = 0;
int j = A.length - 1; while (i <= j)
{
int mid = (int)((i + j)/2);
if (A[mid] == target)
return mid;
else if (A[mid] > target)
j = mid - 1;
else
i = mid + 1;
}
return i;
}
}</span>

[LeetCode][Java] 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. [array] leetcode - 35. Search Insert Position - Easy

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

  3. LeetCode 035 Search Insert Position

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

  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 for LeetCode 035 Search Insert Position

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

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

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

  8. LeetCode 35 Search Insert Position(查找插入位置)

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

  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. ZH奶酪:PHP如何判断提交表单中多个复选框是否选中?

    1.name命名为数组,例如“select[]” 2.例如这样选: 3.在后台使用$_POST['select']得到数组 4.然后就可以看到得到的数组了 原文链接:http://www.zhihu. ...

  2. Node使用淘宝 NPM 镜像

    npm install -g cnpm --registry=https://registry.npm.taobao.org之后可以通过cnpm来安装node模块cnpm install [name]

  3. hadoop safemode error

    http://www.cnblogs.com/hustcat/archive/2010/06/30/1768506.html 1.safemode bin/hadoop fs -put ./input ...

  4. Hadoop,HBase集群环境搭建的问题集锦(二)

    10.艾玛, Datanode也启动不了了? 找到log: Caused by: java.net.UnknownHostException: Invalid host name: local hos ...

  5. html 语义化标签拾遗

    1.del和ins标签 兼容性:浏览器全部支持 del:定义文档中已被删除的文本. ins:定义已经被插入文档中的文本. <!DOCTYPE html> <html lang=&qu ...

  6. 【Linux】echo命令

    用途 echo是用于终端打印的基本命令 说明 只需要使用带双引号的文本,结合echo命令就可以将文本打印在终端. [root@localhost test]# echo "Hello Wor ...

  7. Oracle EBS WMS功能介绍(二)

    Oracle EBS WMS功能介绍(二) (版权声明,本人原创或者翻译的文章如需转载,如转载用于个人学习,请注明出处.否则请与本人联系,违者必究) 出货物流逻辑主要包括 1.      打包.能够进 ...

  8. 微信小程序:酒店订房之时间选择器 picker

    下单页面,选择开始日期和结束日期,不废话,直接代码: 1.wxml: <picker mode="date" value="{{date1}}" star ...

  9. Apache-支持shtml实现include文件解析的配置方法

    1. 确认加载include.so模块,将注释去掉: LoadModule include_module libexec/apache2/mod_include.so 2. AddType部分去掉这两 ...

  10. 计算机组成原理实验之CPU组成与指令周期实验

    (实验五  CPU组成与指令周期实验) 课程 计算机组成原理实验 实验日期 2015 年 12 月  8 日 一.实验目的 1.将微程序控制器同执行部件(整个数据通路)联机,组成一台模型计算机. 2. ...