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

如题所述,最直观的做法就是二分查找。不过在使用二分查找解决此问题时,需要多加小心,先考虑清楚所有情况,再开始编码。

首先考虑边界:

  1. 若给定target小于数组第一个元素,返回0;
  2. 若target大于最后一个元素,返回n

使用二分,结束循环条件应该是high-low=1的情况。

例如[1,3,5,6]中查找2,二分一次后,low=0,high=1,此时A[low]=1,A[high]=3。

若按照平常使用的二分查找就应该找不到元素exit了,但是要返回元素的值则需要再进一步处理,此时low+1,或high-1即为元素应该的位置。

下面代码里我把target等于边界值(数组第1个和第n个)的情况放到最后比较了。

 class Solution {
public:
int searchInsert(int A[], int n, int target) {
if(A == NULL || n < )
return -;
if(target < A[])
return ;
if(target > A[n-])
return n; int low = ;
int high = n-; int mid = ; while(high-low>){
mid = low + (high - low)/;
if(A[mid] == target)
return mid;
else if(A[mid] > target)
high = mid;
else
low = mid;
} if(high-low ==)
if(A[low] == target)
return low;
if(A[high] == target)
return high;
else
return low+; }
};

Search Insert Position 查找给定元素在数组中的位置,若没有则返回应该在的位置的更多相关文章

  1. Search insert position, 查找插入位置

    问题描述:给定一个有序序列,如果找到target,返回下标,如果找不到,返回插入位置. 算法分析:依旧利用二分查找算法. public int searchInsert(int[] nums, int ...

  2. C语言-查找一个元素在数组中的位置

    #include<stdio.h> #include <stdlib.h> #include <time.h> int search(int key, int a[ ...

  3. Leetcode之二分法专题-35. 搜索插入位置(Search Insert Position)

    Leetcode之二分法专题-35. 搜索插入位置(Search Insert Position) 给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引.如果目标值不存在于数组中,返回它将会 ...

  4. [LeetCode][Java] Search Insert Position

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

  5. Leetcode 二分查找 Search Insert Position

    本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Search Insert Position Total Accepted: 14279 T ...

  6. LeetCode:Search Insert Position,Search for a Range (二分查找,lower_bound,upper_bound)

    Search Insert Position Given a sorted array and a target value, return the index if the target is fo ...

  7. LeetCode 35 Search Insert Position(查找插入位置)

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

  8. LeetCode Search Insert Position (二分查找)

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

  9. [LC]35题 Search Insert Position (搜索插入位置)

    ①英文题目 Given a sorted array and a target value, return the index if the target is found. If not, retu ...

随机推荐

  1. createQuery与createSQLQuery区别

    该篇文章也贴上来: hibernate 中createQuery与createSQLQuery两者区别是:前者用的hql语句进行查询,后者可以用sql语句查询前者以hibernate生成的Bean为对 ...

  2. Compile git version inside go binary

    Compile git version inside go binary Abstract 在我们编写的程序中总是希望可以直接查阅程序的版本,通过--version参数就会输出如下版本信息. Buil ...

  3. net与树莓派的情缘-安装SVN(三)

    sudo apt-get install subversion cd /home/pi mkdir svn svnadmin create /home/pi/svn/Project cd /home/ ...

  4. (转)MySQL多源复制

    原文:https://dev.mysql.com/doc/refman/5.7/en/replication-multi-source.html MySQL多源复制概述 MySQL多源复制使复制从接受 ...

  5. Linux网络编程服务器模型选择之并发服务器(下)

    前面两篇文章(参见)分别介绍了循环服务器和简单的并发服务器网络模型,我们已经知道循环服务器模型效率较低,同一时刻只能为一个客户端提供服务,而且对于TCP模型来说,还存在单客户端长久独占与服务器的连接, ...

  6. linux下RabbitMQ相关命令

    1. 关闭与启动 ① 到指定目录:cd/etc/init.d ② 停止:rabbitmq-server stop ③ 启动:rabbitmq-server start ④ 查看是否停止/启动成功:ps ...

  7. DIV盒子介绍

    1.盒子模型=网页布局的基石,由四部分组成: 边框(border).外边距(margin).内边距(padding).盒子中的内容(content) 2.设置顺序是顺时针:上.右.下.左. 三个值(上 ...

  8. LeetCode-2. Add Two Numbers(链表实现数字相加)

    1.题目描述 You are given two non-empty linked lists representing two non-negative integers. The digits a ...

  9. str_split 分隔中文出现乱码 替代函数

    function mbstringtoarray($str,$charset) { $strlen=mb_strlen($str); while($strlen){ $array[]=mb_subst ...

  10. 转载:GitHub 新手详细教程

    GitHub 新手详细教程 https://blog.csdn.net/Hanani_Jia/article/details/77950594