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


题目标签:Array

  这道题目给了我们一个有序序列,让我们找到target的位置,如果没有target,找到有序嵌入target的位置。利用二分法,来找target,如果没有target,那么最后返回left,就是我们需要嵌入的位置。

  我们来根据code 看原题的例子,当mid < target的话,left 等于 mid + 1; while 条件是包括 left = right。

  [1,3,5,6], 2 -> mid = 3,3 大于2的, 那么范围缩小到 1这个数字, left 和 right 都指向1。1是大于2的,那么left = mid + 1, left 就指向了3, 并且while loop 结束了。 3这个位置就是需要嵌入的地方。

  [1,3,5,6], 7 -> mid = 3, 3 小于 7 的, 那么范围缩小到 left 指向5, right 指向6。 mid = 5, 依然小于7, 那么 left = mid + 1,  left 指向6, right 也指向6。mid = 6, 再一次小于7, left = mid + 1, 就超出了范围,但是这个时候left 指向的是6 后面一位,就是嵌入的位置。

  [1,3,5,6], 0 -> mid = 3 大于0, right = mid - 1; left 指向1, right 指向1。mid = 1, 依然大于0, right = mid - 1;这时候right 已经等于 -1 了。超出了范围,但是left 依然是 0, 而这个 index = 0 的位置就是需要嵌入的位置。

  根据这三个例子的特性,要嵌入的位置,在array 左边是不能超出的,右边是可以超出的。所以我们只要最后return left 就可以了。因为left 只会超过右边。

Java Solution:

Runtime beats 70.88%

完成日期:03/28/2017

关键词:Array

关键点:二分法

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

参考资料:N/A

LeetCode 算法题目列表 - LeetCode Algorithms Questions List

LeetCode 35. Search Insert Position (搜索嵌入的位置)的更多相关文章

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

  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] 35. Search Insert Position 解决思路

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

  4. 【LeetCode】Search Insert Position(搜索插入位置)

    这道题是LeetCode里的第35道题. 题目描述: 给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引.如果目标值不存在于数组中,返回它将会被按顺序插入的位置. 你可以假设数组中无重复元 ...

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

  6. Leetcode 35 Search Insert Position 二分查找(二分下标)

    基础题之一,是混迹于各种难题的基础,有时会在小公司的大题见到,但更多的是见于选择题... 题意:在一个有序数列中,要插入数target,找出插入的位置. 楼主在这里更新了<二分查找综述>第 ...

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

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

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

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

随机推荐

  1. birt 集成到现有的web应用中

     我们已经有了一个Javaweb应用,现在要实现对报表的集成 我的应用是这个样子的  说明: 1)  这里使用的是birt4.4版本的, 下载birt-runtime-4.4.zip(在官方下载),然 ...

  2. Flask-WTF 创建表单P2

    表单安全 无需任何配置,FlaskForm将提供具有CSRF(Cross-site request forgery,也被称为one-click attack 或者session riding,通常缩写 ...

  3. OC——多态

    书接上文,上文提到继承一个很大用途的是为了更好的实现多态,现在我们就来看看OC的多态. 多态:顾名思义就是好多种状态,以前学C#时候印象最深刻的例子是好多个类共同实现同一个接口,然后把这些类的对象都装 ...

  4. C/C++ 进程通讯(命名管道)

    服务端代码: // pipe_server.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <stdio.h> ...

  5. CA认证和颁发吊销证书

    摘要:涉及到网络安全这一块,想必大家都听过CA吧.像百度.淘宝.京东等这些知名网站,每年都要花费一笔money来买CA证书.但其实简单的企业内的CA认证,我们自己就可以实现,今天小编我就讲解一下怎么在 ...

  6. matplotlib学习之绘图基础

    matplotlib:http://www.cnblogs.com/jasonhaven/p/7609059.html 1.基本图形 散点图:显示两组数据的值,每个点的坐标位置由变量的值决定,头一组不 ...

  7. 搭建dubbo+zookeeper+dubboadmin分布式服务框架(windows平台下)

    1.zookeeper注册中心的配置安装 1.1 下载zookeeper包(zookeeper-3.4.6.tar.gz),ZooKeeper是一个分布式的,开放源码的分布式应用程序协调服务,是Goo ...

  8. Vim的基本使用(一)

    本文为原创文章,转载请标明出处 目录 1.移动光标 2.屏幕滚动 3.模式查找 4.位置标记 5.删除文本 6.撤销与重做 7.插入文本 8.复制与移动 9.修改文本 10.写入与退出 1. 移动光标 ...

  9. linux下快速列出文件列表的方法

    前言 这两天碰到一个很棘手的问题,需要读取出ubuntu系统中某个目录下所有文件,由于服务器中存储的文件实在太多,导致此过程效率十分低下,动辄需要等待一个小时之久,还只是一个目录.于是如何快速获取文件 ...

  10. VB.NET生成重复窗体

    Public Class Form1 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click ...