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 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 (搜索嵌入的位置)的更多相关文章
- [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 ...
- [array] leetcode - 35. Search Insert Position - Easy
leetcode - 35. Search Insert Position - Easy descrition Given a sorted array and a target value, ret ...
- [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 ...
- 【LeetCode】Search Insert Position(搜索插入位置)
这道题是LeetCode里的第35道题. 题目描述: 给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引.如果目标值不存在于数组中,返回它将会被按顺序插入的位置. 你可以假设数组中无重复元 ...
- [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 ...
- Leetcode 35 Search Insert Position 二分查找(二分下标)
基础题之一,是混迹于各种难题的基础,有时会在小公司的大题见到,但更多的是见于选择题... 题意:在一个有序数列中,要插入数target,找出插入的位置. 楼主在这里更新了<二分查找综述>第 ...
- 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 ...
- [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 ...
- LeetCode 35 Search Insert Position(查找插入位置)
题目链接: https://leetcode.com/problems/search-insert-position/?tab=Description 在给定的有序数组中插入一个目标数字,求出插入 ...
随机推荐
- Mybatis第一篇【介绍、快速入门、工作流程】
什么是MyBatis MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google code,并且改名为 ...
- Servlet知识点大纲
这是我整理的Servlet知识点大纲,可按照它的顺序来学习-..具体的内容在我的博客中都可以找到!
- Hibernate的注解方法的使用
1.配置映射关系的xml方式 我们知道,Hibernate是一个典型的ORM框架,用以解决对象和关系的不匹配.其思想就是将关系数据库中表的记录映射成为对象,以对象形式展现,这样一来,就可以把对数据库的 ...
- response对象的使用
使用response对象提供的sendRedirect()方法可以将网页重定向到另一个页面.重定向操作支持将地址重定向到不同的主机上,这一点与转发是不同的.在客户端浏览器上将会得到跳转地址,并重新发送 ...
- Apache Spark 2.2.0 中文文档 - Spark RDD(Resilient Distributed Datasets)论文 | ApacheCN
Spark RDD(Resilient Distributed Datasets)论文 概要 1: 介绍 2: Resilient Distributed Datasets(RDDs) 2.1 RDD ...
- hadoop2.0的数据副本存放策略
在hadoop2.0中,datanode数据副本存放磁盘选择策略有两种方式: 第一种是沿用hadoop1.0的磁盘目录轮询方式,实现类:RoundRobinVolumeChoosingPolicy.j ...
- DelayQueue使用示例之KTV包厢记时
在学习Java 多线程并发开发过程中,了解到DelayQueue类的主要作用:是一个无界的BlockingQueue,用于放置实现了Delayed接口的对象,其中的对象只能在其到期时才能从队列中取走. ...
- [python学习笔记] 开篇
今天开始学习markdown,立贴为证. 平均一天一小时.两个月先学完基础. 所有的帖子只为了学习记录,有不对的请提出,轻吐槽
- Dubbo与Zookeeper、SpringMVC整合和使用
作为dubbo框架初学者,能让框架跑起来非常不容易,非常感谢网上诸多大神提供的文章,本人参考文章地址是:https://my.oschina.net/xshuai/blog/891281 不过别人的记 ...
- 为什么要用深度学习来做个性化推荐 CTR 预估
欢迎大家前往腾讯云技术社区,获取更多腾讯海量技术实践干货哦~ 作者:苏博览 深度学习应该这一两年计算机圈子里最热的一个词了.基于深度学习,工程师们在图像,语音,NLP等领域都取得了令人振奋的进展.而深 ...