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. 整数的故事(4)——Karastuba算法

    我们在小学就学过用竖式计算两个多位数的乘法: 这个过程简单而繁琐,没有最强大脑的普通大众通常是用计算器代替的.然而对于超大整数的乘法,计算器也未必靠得住,它还存在“溢出”一说.这就需要我们自行编写算法 ...

  2. Linux(Centos7)下搭建SVN服务器

    操作系统: CentOS 7.6 64位 第一步:通过yum命令安装svnserve,命令如下: 检测svn是否安装: rpm -qa subversion #检查现有版本,如果输入命令后没有提示的话 ...

  3. string formating字符串格式化,function函数,group组,recursion递归,练习

    # -*- coding: UTF-8 -*- msg = 'i am {} my hobby is {}'.format('lhf',18) print(msg) msg1 = 'i am %s m ...

  4. 如何修改MSSQL的用户名

    Alter LOGIN sa DISABLE Alter LOGIN sa WITH NAME = [systemAccount] "systemAccount" 为SA的新名称, ...

  5. kafka集群的错误处理--kafka一个节点挂了,导致消费失败

    今天由于kafka集群搭建时的配置不当,由于一台主消费者挂掉(服务器崩了,需要维修),导致了所有新版消费者(新版的offset存储在kafka)都无法拉取消息. 由于是线上问题,所以是绝对不能影响用户 ...

  6. Centos7 安装redis集群哨兵模式

    https://blog.csdn.net/lihongtai/article/details/82826809

  7. sqlserver to oracle

    SELECT c.*, d .Organization_Name, d .ParentId, e.Roles_ID, e.Roles_Name FROM ( SELECT a.*, b.Organiz ...

  8. Linux下安装GitHub

    安装GitHub 1.下载安装git: yum -y install git git-core git-doc 2.运行完执行 ssh-keygen -t rsa -C "your@emai ...

  9. RestExpress response中addHeader 导致stackOverflow

    问题描述: 最近在项目使用中要在restExpress的header中增加一个键值对,同事在使用的时候没有对header的value进行非空判断,于是在测试环境测试的时候就出现了一个异常

  10. leetcode448

    public class Solution { public IList<int> FindDisappearedNumbers(int[] nums) { Dictionary<i ...