题目:

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奶酪:putty远程登录Linux服务器非常慢

    11.pytty远程登录Linux服务器非常慢 http://www.it165.net/os/html/201209/3425.html 12.启动SSHD服务报错 http://blog.chin ...

  2. HBase数据迁移至Hive

    背景:需要将HBase中表xyz(列簇cf1,列val)迁移至Hive 1. 建立Hive和HBase的映射关系     1.1 运行hive shell进入hive命令行模式,运行如下脚本 CREA ...

  3. Java从零开始学十二(构造方法)

    一.构造方法 构造方法的主要目的是为类中的属性初始化 二.构造方法的定义格式 class 类名称{ 访问权限 类名称(类型1 参数1,类型2 参数2,…){ 程序语句 ; …    // 构造方法没有 ...

  4. python中的静态成员方法和类成员方法

    Python的静态方法和类成员方法都可以被类或实例访问,两者概念不容易理清,但还是有区别的: 1)静态方法无需传入self参数,类成员方法需传入代表本类的cls参数: 2)从第1条,静态方法是无法访问 ...

  5. Menubar

    A menubar is a common part of a GUI application. It is a group of commands located in various menus. ...

  6. maven的下载以及安装

    1.下载Maven 打开链接:https://maven.apache.org/download.cgi 下载好之后解压到一个盘,我的是解压到F:\Maven\apache-maven-3.5.3-b ...

  7. JDK核心JAVA源代码解析(1) - Object

    想写这个系列非常久了,对自己也是个总结与提高.原来在学JAVA时.那些JAVA入门书籍会告诉你一些规律还有法则,可是用的时候我们一般非常难想起来,由于我们用的少而且不知道为什么.知其所以然方能印象深刻 ...

  8. sqlplus命令手冊

    show和set命令是两条用于维护SQLPlus系统变量的命令 : SQL> show all --查看全部系统变量值 SQL>show user --显示当前连接用户 SQL>sh ...

  9. 快速搭建Seeddms文档管理系统

    Seddms文档管理系统是开源的 环境: Redhat6.5 lamp 01.LAMP的安装 安装请看:http://www.cnblogs.com/xiaochina/p/6442337.html ...

  10. aop注解 spring提供的事务

    http://www.cnblogs.com/friends-wf/p/3826893.html 是 自定义的切面,并且添加注解 声明为切面 利用spring提供的事务声明 主要在 service层上 ...