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. What’s New In GRANDstack?

    转自:https://blog.grandstack.io/whats-new-in-grandstack-310c067fea4a There’s been a lot of activity in ...

  2. tcp_timestamps和tcp_tw_recycle

    不同时开启tcp_timestamps和tcp_tw_recycle的场景描述 FULL NAT下 FULL NAT  在client请求VIP 时,不仅替换了package 的dst ip,还替换了 ...

  3. Tomcat虚拟根目录与虚拟目录

    tomcat版本:apache-tomcat-7.0.42 参考:http://blog.csdn.net/pangdingshan/article/details/7214786 一.虚拟根目录 1 ...

  4. RouterOS限速更高级运用

    转自这里 一般我们用ros限速只是使用了max-limit,其实ros限速可以更好的运用.比如我们希望客户打开网页时速度可以快一些,下载时速度可以慢一些.ros2.9就可以实现. 看图片 max-li ...

  5. 4.IIC总线

    一.IIC总线说明:      IIC总线时序只有高低电平的持续时间一般是大于多少us/ms.      iic时序:            开始:当SCL为高电平时,SDA由高电平状态切换到低电平状 ...

  6. 报错:NoSuchMethodError: kafka.javaapi.PartitionMetadata.leader()Lkafka/cluster/Broker;

    报错现象: 在pom文件添加: <dependency> <groupId>org.apache.kafka</groupId> <artifactId> ...

  7. ef中文文档

    https://entityframework.net/zh-CN/home 在使用ef进行对数据库操作时 数据库迁移 https://www.dotnettricks.com/learn/entit ...

  8. 20165312 2017-2018-2《Java程序设计》课程总结

    20165312 2017-2018-2<Java程序设计>课程总结 每周作业链接汇总 预备作业1:我期望的师生关系 预备作业2:C语言基础调查和java学习展望 预备作业3:Linux安 ...

  9. Django_admin组件

    1.Django_admin组件的意义 作者:Eric 微信:loveoracle11g 新建Django项目bms图书管理系统 App为book book/models.py添加表关系 from d ...

  10. html/css/js-个人容易忘的一些属性

    1.当div里面的文字超过给定div给定的宽度,div里面的文字自动换行 word-break:break-all:会截断该行最后的单词 word-wrap:break-word:不会截断,该行长度最 ...