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. GSON速学必会

    一. GSON 简介 GSON是一个用Java语言编写的用于处理JSON数据格式的开源应用程序编程接口项目.它将Java对象转换为JSON表示.还可以用于将JSON字符串转换为等效的Java对象. g ...

  2. Babel初体验

    原文地址:→传送门 写在前面 现在es6很流行,尽管各大浏览器都还不能支持它的新特性,但是小伙伴们还是很中意它呀,于是小小的学习的一下 Babel 这里我们不介绍es6相关内容,只是说下入坑前奏,记录 ...

  3. smtp模块使用

    import smtplib from email.mime.text import MIMEText from bs4 import BeautifulSoup from urllib.reques ...

  4. jstl-初步认知

    JSTL是java提供的JSP标签库 1,在项目中加入 jsf-api.jar jsf-impl.jar jstl-1.2.jar 三个包 2, 如何在jsp页面引入标签库 使用 <@tagli ...

  5. 关于APP分享到QQ、微信等

    <script> var shares=null;        var Intent=null,File=null,Uri=null,main=null; function plusRe ...

  6. UEditor1.4.3.3整合Spring MVC和七牛

    [前言] 项目中涉及将UEditor上传服务器整合进已有的基于Spring MVC的服务中,并且将上传到本地改为上传到七牛,看似简单的一个需求,实际做起来还是遇到了一些困难.在这里分享一下经验-- 七 ...

  7. PHP垃圾回收机制理解

    使用的是"引用计数"方式进行回收.简单地理解的话,就是每个分配的内存区域都有一个计数器,记录有多少个变量指针指向这片内存.当指向该片内存的指针数量为0,那么该片内存区域就可以被回收 ...

  8. asp.net core后台系统登录的快速构建

    登录流程图 示例预览 构建步骤 当然,你也可以直接之前前往coding仓库查看源码,要是发现bug记得提醒我啊~ LoginDemo地址 1. 首先你得有一个项目 2. 然后你需要一个登录页面 完整L ...

  9. Linux入门之常用命令(13) crontab

    为当前用户创建cron服务 1.  键入 crontab  -e 编辑crontab服务文件 例如 文件内容如下: */2 * * * * /bin/sh /home/admin/jiaoben/bu ...

  10. 【BZOJ】1015 [JSOI2008]星球大战starwar(并查集+离线处理)

    Description 很久以前,在一个遥远的星系,一个黑暗的帝国靠着它的超级武器统治者整个星系.某一天,凭着一个偶然的机遇,一支反抗军摧毁了帝国的超级武器,并攻下了星系中几乎所有的星球.这些星球通过 ...