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 ...
 
随机推荐
- javaweb Servlet接收Android请求,并返回json数据
			
1.实现功能 (1)接收http请求 (2)获取Android客户端发送的参数对应的内容 (3)hibernate查询数据库 (4)返回json数据 2.java代码 import EntityCla ...
 - Codeforces Global Round 2 部分题解
			
F.Niyaz and Small Degrees 挺sb的一题,为什么比赛时只过了4个呢 考虑当\(x\)固定的时候怎么做.显然可以树形DP:设\(f_{u,i=0/1}\)表示只考虑\(u\)子树 ...
 - 求幂大法,矩阵快速幂,快速幂模板题--hdu4549
			
hdu-4549 求幂大法.矩阵快速幂.快速幂 题目 M斐波那契数列 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 ...
 - 查看APK包签名的方法。
			
1.查看 keystore $ keytool -list -keystore debug.keystore 结果: Keystore type: JKS Keystore provider: SUN ...
 - 更换bbr内核
			
1:首先yum update -y更新到最新CentOS 7.3 1611cat /etc/redhat-releaseCentOS Linux release 7.3.1611 (Core) 2: ...
 - 11.Set 和 Map数据结构
			
1.set 基本用法 ES6 提供了新的数据结构 Set.它类似于数组,但是成员的值都是唯一的,没有重复的值. Set 本身是一个构造函数,用来生成 Set 数据结构. const s = new S ...
 - Java类文件结构及javac的ClassReader类解读
			
首先来看一下ClassFile,类注释如下: A JVM class file. Generic Java classfiles have one additional attribute for c ...
 - [C语言]链表实现贪吃蛇及部分模块优化
			
在继上篇[C语言]贪吃蛇_结构数组实现大半年后,链表实现的版本也终于出炉了.两篇隔了这么久除了是懒癌晚期的原因外,对整个游戏流程的改进,模块的精简也花了一些时间(都是借口). 优化模块的前沿链接: · ...
 - getSqlMapClientTemplate().insert()方法的返回值问题
			
insert方法的返回值 今天碰到一个问题,就是关于ibatis的insert方法的返回值的问题.在网上找了很多例子,自己也亲自试了一下. 最后得出结论:insert方法返回的是在表中插入记录 ...
 - Maven 打包的时候报 Failed to execute goal org.codehaus.mojo:native2ascii-maven-plugin
			
错误信息: [ERROR] Failed to execute goal org.codehaus.mojo:native2ascii-maven-plugin:1.0-alpha-1:native2 ...