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 在给定的有序数组中插入一个目标数字,求出插入 ...
随机推荐
- 多线程面试题系列(3):原子操作 Interlocked系列函数
上一篇中讲到一个多线程报数功能.为了描述方便和代码简洁起见,我们可以只输出最后的报数结果来观察程序是否运行出错.这也非常类似于统计一个网站每天有多少用户登录,每个用户登录用一个线程模拟,线程运行时会将 ...
- Atlas框架介绍集成(一)
Atlas是什么? Atlas是一个Android客户端容器框架,主要提供了组件化.动态性.解耦化的支持.支持在编码期.Apk运行期以及后续运维修复期的各种问题. 在工程期,实现工程独立开发,调试功能 ...
- AnsiString和String的区别、使用
16.C/C++语言在CB中的一些特定用法 2)AnsiString是从Delphi中引进来的吗? 答:CB的核心组件VCL是用Object Pascal语言写出的,所以CB的VCL组件的属性有很多都 ...
- 打印ASCII码
总时间限制:1000ms内存限制:65536kB 描述 输入一个除空格以外的可见字符(保证在函数scanf中可使用格式说明符%c读入),输出其ASCII码. 输入 一个除空格以外的可见字符. 输出 一 ...
- POJ-2417-Discrete Logging(BSGS)
Given a prime P, 2 <= P < 2 31, an integer B, 2 <= B < P, and an integer N, 1 <= N &l ...
- UWP 改变Button样式
-----some words------ 1.Control:控制 (我们理解成控件) 2.Template:模板 3.Ellipse 椭圆 4.Content 内容 5.Presenter 节目主 ...
- 寻找bug并消灭系列——记录在Android开发所遇到的bug(二)
bug 1: bug描述: 无法成功地将edittext中的内容传入数据库中 bug动图: 经过: 最近写了个项目,项目要使用到SQL数据库,由于没有相关知识,便是找到了各种资料开始了自学之旅,在de ...
- Python自学笔记-paramiko模块(Mr serven)
文章出处:http://www.cnblogs.com/wupeiqi/articles/5095821.html SSHClient 用于连接远程服务器并执行基本命令 基于用户名密码连接: #!/u ...
- Python 基础系列一:初识python
为什么是Python而不是其他语言? C 和 Python.Java.C#等 C语言: 代码编译得到 机器码 ,机器码在处理器上直接执行,每一条指令控制CPU工作. 其他语言: 代码编译得到 字节码 ...
- HDU2036 改革春风吹满地
第一次看到这题果断放弃,毕竟几何白痴,第二次刷没做的题的时候突然想到这个三角形面积的向量法:S=|x1*y2-x2*y1| 但是此题可能是凹多边形,所以不能加绝对值,可以画个凹四边形看看. HDU2 ...