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 ...
随机推荐
- 【MPI】执行mpiexec出错
运行mpiexec提示mpiexec_mic: cannot connect to local mpd (/tmp/mpd2.console_jzhang); possible causes: 1. ...
- Nginx PHP fpm forbidden 原因
可能是标红目录层级不一致 location / { root /var/www/html/public; index index.php; } location ~ \.php$ { root /va ...
- media(适配)
媒体类型 1.all 所有媒体 2.braille 盲文触觉设备 3.embossed 盲文打印机 4.print 手持设备 5.projection 打印预览 ...
- 《LeetBook》leetcode题解(9):Palindrome Number[E]——回文数字
我现在在做一个叫<leetbook>的开源书项目,把解题思路都同步更新到github上了,需要的同学可以去看看 地址:https://github.com/hk029/leetcode 这 ...
- Javac之关于方法的调用1
方法的调用从Attr类的visitApply()方法进入,如下: /** Visitor method for method invocations. * NOTE: The method part ...
- Android开源项目xUtils HttpUtils模块分析(转)
xUtils是github上的一个Android开源工具项目,其中HttpUtils模块是处理网络连接部分,刚好最近想整理下Android网络编程知识,今天学习下xUtils中HttpUtils. x ...
- vue引入bootstrap和fontawesome
npm install jquery npm install bootstrap npm install popper.js. import $ from 'jquery' import 'boots ...
- AngularJS 的常用特性(一)
前言:AngularJS 是一款来自 Google 的前端 JS 框架,该框架已经被应用到了 Google 的多款产品中,这款框架最核心特性有:MVC.模块化.自动化双向数据绑定.语义化标签.依赖注入 ...
- P2P 行业解决方案
P2P指个人与个人之间的借贷,而P2P理财是指以公司为中介机构,把这借贷双方对接起来实现各自的借贷需求.借款方可以是无抵押贷款或是有抵押贷款.而中介一般是收取双方或单方的手续费为盈利目的或者是赚取一定 ...
- Java泛型的逆变
在上篇<Java泛型的协变>这篇文章中遗留以下问题——协变不能解决将子类型添加到父类型的泛型列表中.本篇将用逆变来解决这个问题. 实验准备 我们首先增加以下方法,见代码清单1所示. 代码清 ...