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.

Example 1:

Input: [1,3,5,6], 5
Output: 2

Example 2:

Input: [1,3,5,6], 2
Output: 1

Example 3:

Input: [1,3,5,6], 7
Output: 4

Example 4:

Input: [1,3,5,6], 0
Output: 0

题意:

给定有序数组和一个target,寻找合适的插入位置。

Solution1: Binary Search

数组元素有偶数个时, 中点若取偏左端的那个

1      3   4

^mid

那么,更新left时,从mid + 1 开始

code:

 /*
Time: O(log(n))
Space: O(1)
*/
class Solution {
public int searchInsert(int[] nums, int target) {
//corner case
if (nums == null || nums.length == 0) {
return 0;
}
if (target < nums[0]) { //[1,3,5,6], 0
return 0;
} else if (target > nums[nums.length - 1]) { // [1,3,5,6], 7
return nums.length;
}
// binary search
int left = 0, 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;
} else {
left = mid + 1;
}
}
return left;
}
}

[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(查找插入位置)

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

  4. [Leetcode] search insert position 寻找插入位置

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

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

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

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

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

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

  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 (搜索嵌入的位置)

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

随机推荐

  1. UBUNTU中使用pip安装,提示cannt import main问题

    在pip==8.1.1版本中,使用pip install Django==1.8.16时,提示 Traceback (most recent call last):  File "/usr/ ...

  2. 根据CPU核心数确定线程池并发线程数

    一.抛出问题 关于如何计算并发线程数,一般分两派,来自两本书,且都是好书,到底哪个是对的?问题追踪后,整理如下: 第一派:<Java Concurrency in Practice>即&l ...

  3. [转]马上2018年了,该不该下定决心转型AI呢

    转自:http://blog.csdn.net/eNohtZvQiJxo00aTz3y8/article/details/78941013 2017年,AI再一次迈向风口,但我们如何判断未来走向?应不 ...

  4. mnist全连接层网络权值可视化

    一.数据准备 网络结构:lenet_lr.prototxt 训练好的模型:lenet_lr_iter_10000.caffemodel 下载地址:链接:https://pan.baidu.com/s/ ...

  5. 文件处理,三元操作符,seek()函数,迭代函数和列表解析,reduce函数

    1.文件读取方类型 r,r+,w,x,a, r,读文件 w,写文件,文件内容全部删除,并将新内容从第一行开始赋值 x,写文件,只有文件不存在,可写,文件存在,报错 a,在文件莫问追加信息 r+,w+, ...

  6. 搜狗浏览器总是打开123.sogou.com-记搜狗浏览器遭遇劫持一例

    昨日,因从网上下载了office2010破解工具,压缩包中有个文件为名为[office 2010激活工具\为保证永久激活,要先点击这个配置,再点击KMSELDIYI激活.exe],单击之后没有反应.后 ...

  7. 洛谷P1636学画画

    传送 这个题我们需要一个大胆的想法(虽然AC后看了题解知道这是个定理) (求证明qwq) 如果一个图有2或0个奇点,它就一定可以一笔画出,如果不是2或0个奇点,那答案就是奇点数/2 (私认为因为两个奇 ...

  8. springMVC配置文件web.xml与spring-servlet.xml与spring-jdbc.xml与logback.xml与redis.properties与pom.xml

    springMVC注解:@Controller @Service @Repository 分别标注于web层,service层,dao层. web.xml <?xml version=" ...

  9. HOOK - 低级鼠标Hook

    参考博客 一.SetWindowsHookEx HHOOK WINAPI SetWindowsHookEx( __in int idHook, \\钩子类型 __in HOOKPROC lpfn, \ ...

  10. python之路——9

    王二学习python的笔记以及记录,如有雷同,那也没事,欢迎交流,wx:wyb199594 学习内容 1.文件处理 1.打开文件 open(‘路径’,‘打开方式’) 打开方式 r w a r+ w+ ...