【题解】【数组】【查找】【Leetcode】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
思路:
其实二分查找是最基本的,本来没什么可说的,要命的是谈虎色变,条件稍微一变,一紧张边界情况就想不清楚大脑内存耗尽,写Search a 2D Matrix的时候,调了半天才发现程序是在二分处理只有两个元素的时挂了,写Median of Two Sorted Arrays的时候,又发现如果只有两个元素可能会造成死循环,所以担惊受怕的某程序媛决定采用这样的保守写法,小心地呵护trivial case,慎而试run之,大惊Accept
int searchInsert(int A[], int n, int target) {
int s = ;
int e = n-;
while(s < e- ){
int mid = (s+e)/;
if(target == A[mid]){
return mid;
}else if(target > A[mid]){
s = mid + ;
}else if(target < A[mid]){
e = mid - ;
}
}
if(s == e-){
if(target <= A[s]) return s;
else if(target > A[s+]) return s+;
else return s+;
}else if(s == e){
if(target <= A[s]) return s;
else return s+;
}
}
为了不愧对大脑内存正常的同学,奉上干货Matrix67的两篇文章《漫话二分》,并贴出一种正常的写法。。。跟经典的二分查找相比,只是多了一个条件:
int searchInsert(int A[], int n, int target) {
int l = , r = n-;
while(l <= r){
int mid = (l+r)/;
if(target == A[mid])
return mid;
if(mid > l && target < A[mid] && target > A[mid-])//比二分查找仅仅多了这句
return mid;
if(target < A[mid]){
r = mid-;
}else{
l = mid+;
}
}
return l;
}
【题解】【数组】【查找】【Leetcode】Search Insert Position的更多相关文章
- 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: Search Insert Position 解题报告
Search Insert Position Given a sorted array and a target value, return the index if the target is fo ...
- LeetCode 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 (二分查找)
题意: 给一个升序的数组,如果target在里面存在了,返回其下标,若不存在,返回其插入后的下标. 思路: 来一个简单的二分查找就行了,注意边界. class Solution { public: i ...
- [LeetCode] Search Insert Position 搜索插入位置
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
- LeetCode——Search Insert Position
Description: Given a sorted array and a target value, return the index if the target is found. If no ...
- [Leetcode] search insert position 寻找插入位置
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
- [LeetCode] Search Insert Position 二分搜索
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
- [LeetCode] Search Insert Position
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
- leetcode Search Insert Position Python
#Given a sorted array and a target value, return the index if the target is found. If #not, return t ...
随机推荐
- Tomcat配置虚拟主机的两种方式
1.基于主机名的虚拟主机配置 在随意盘符下建立一个目录作为虚拟地址的目录.例如:F:\virtualhost1,在其下建立 test1.html,写入内容例如:test 在tomcat/conf/se ...
- [转]使用CSS3实现树形控件
下面是一个使用HTML的ul标签制作的关于国家区划的组织结构图. 中国 北京 广东省 广州市 韶关市 海南省 海口市 美兰区 龙华区 秀英区 琼山区 三亚市 安徽省 合肥市 安庆市 United St ...
- php和AJAX用户注册演示程序
<! doctype html public "-//w3c//dtd html 4.0//en" "http://www.w3.org/tr/rec-html14 ...
- Facebook技术架构
Facebook MySQL,Multifeed (a custom distributed system which takes the tens of thousands of updates f ...
- bzoj 1877: [SDOI2009]晨跑
#include<cstdio> #include<iostream> #include<cstring> #define M 6009 #define inf 2 ...
- C++之函数fgetc和fputc、fgets和fputs、fread和fwrite、fscanf和fprintf用法小结
#include <iostream> #include <cstdio> #include <cstdlib> using namespace std; int ...
- windows-docker开发我常用命令 docker-machine ssh default
docker-machine ssh default docker logs test sudo systemctl start docker docker tag IMAGEID ne ...
- Codeforce385C 树状数组+素因子分解
题目大意: 给多个区间的询问,在询问区间内每一个出现的素数去计算所有数中有多少个数能被这个素数整除 然后将所有素数得到的对应值求和 这里因为初始给定的数不超过10000000,最多670000不到的素 ...
- PowerShell并发控制-命令行参数之四问
传教士问: win下如何 获取进程命令行,及命令行参数? 传教士答: 可以用这个powershell命令(实际上是wmi查询): (get-wmiobject -query "select ...
- S5PV210之添加缺少的-内核提供的'.h'文件 linux3.0.8驱动
怎样解决编译时出现内核提供的函数或变量没有定义,使用source insight搜索功能找到声明的头文件,然后包含该头件就行了: 比如: error: implicit declaration of ...