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
如题所述,最直观的做法就是二分查找。不过在使用二分查找解决此问题时,需要多加小心,先考虑清楚所有情况,再开始编码。
首先考虑边界:
- 若给定target小于数组第一个元素,返回0;
- 若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 查找给定元素在数组中的位置,若没有则返回应该在的位置的更多相关文章
- Search insert position, 查找插入位置
问题描述:给定一个有序序列,如果找到target,返回下标,如果找不到,返回插入位置. 算法分析:依旧利用二分查找算法. public int searchInsert(int[] nums, int ...
- C语言-查找一个元素在数组中的位置
#include<stdio.h> #include <stdlib.h> #include <time.h> int search(int key, int a[ ...
- Leetcode之二分法专题-35. 搜索插入位置(Search Insert Position)
Leetcode之二分法专题-35. 搜索插入位置(Search Insert Position) 给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引.如果目标值不存在于数组中,返回它将会 ...
- [LeetCode][Java] Search Insert Position
题目: Given a sorted array and a target value, return the index if the target is found. If not, return ...
- Leetcode 二分查找 Search Insert Position
本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Search Insert Position Total Accepted: 14279 T ...
- 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 ...
- LeetCode 35 Search Insert Position(查找插入位置)
题目链接: https://leetcode.com/problems/search-insert-position/?tab=Description 在给定的有序数组中插入一个目标数字,求出插入 ...
- LeetCode Search Insert Position (二分查找)
题意 Given a sorted array and a target value, return the index if the target is found. If not, return ...
- [LC]35题 Search Insert Position (搜索插入位置)
①英文题目 Given a sorted array and a target value, return the index if the target is found. If not, retu ...
随机推荐
- 将python的代码文件打包成可执行文件
1.使用pip install Pyinstaller 命令安装 2.使用命令 pyinstaller -F *.py打包成exe 3.在\dist文件夹下找到exe; 一.pyinstaller ...
- Add:四则运算
输入为四则运算表达式,仅由整数.+.-.*./ .(.) 组成,没有空格,要求求其值.假设运算符结果都是整数 ."/"结果也是整数 表达式 由 项 或 项 ...
- C#方法重载和方法重写的区别
一.重载的条件: 1.必须在同一个类中: 2.方法名必须相同: 3.参数列表不能相同. 二.重写的条件: 1. 在不同的类中2. 发生方法重写的两个方法返回值,方法名,参数列表必须完全一致(必须具有相 ...
- git 检出项目部分目录(稀疏检出)
git clone 会把整个项目都clone下来,对于大项目git status比较慢,每次pull时候也拉取一些无关的代码或者文件:git可以实现像svn一样检出部分目录 步骤: git clone ...
- PHP之mb_strripos使用
mb_strripos (PHP 4 >= 4.0.6, PHP 5, PHP 7) mb_strrpos - Find position of last occurrence of a str ...
- WPF的System.Windows.Threading.DispatcherTimer的使用(每隔一定的时间重复做某事)
这里使用了一个进度条来展示, 前段代码: <Window x:Class="TimerTest.MainWindow" xmlns="http://schemas. ...
- Nginx 的信号控制
摘自:Nginx服务器初识:Nginx启动.停止与信号控制 名称 功能 说明 HUP 重启 QUIT 从容关闭 TERM 快速关闭 INT 从容关闭 USR1 切换日志文件 通常用在切 ...
- concurrency基础
Runnable 一个执行任务,没有返回值,也不能抛出受检查异常 Callable 一个执行任务有返回值,也能抛出受检查异常 Future 表示执行任务的生命周期,任务的生命周期为:创建,提交,开始, ...
- redis的数据类型(二)string类型
下面讲解value,value包括String.List.Set.Sorted Set.Hash 一.String类型 1.string类型 String是最基本的类型,而且Stirng类型是二 ...
- weblogic.rjvm.PeerGoneException
并发weblogic异常,报错如下: weblogic.rjvm.PeerGoneException: ; nested exception is: weblogic.utils.net.Socket ...