[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
这题其实便是二分搜索,需要考虑没有找到的时候的情况。
#include <iostream>
using namespace std; class Solution {
public:
int searchInsert(int A[], int n, int target) {
if(n==) return ;
// if(target<=A[0]) return 0;
if(A[n-]<target) return n;
int l=,r=n-,m=;
while(l<=r){
m=(l+r)/;
// cout<<l<<" "<<m<<" "<<r<<endl;
if(A[m]==target) return m;
if(A[m]<target) l=m+;
else r=m-;
}
if(target<A[m]) return m;
return m+;
}
}; int main()
{
int a[]={,};
Solution sol;
for(int i=;i<=;i++)
cout<<i<<" "<<sol.searchInsert(a,sizeof(a)/sizeof(int),i)<<endl;
return ;
}
[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 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 ...
- 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
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 (二分查找)
题意: 给一个升序的数组,如果target在里面存在了,返回其下标,若不存在,返回其插入后的下标. 思路: 来一个简单的二分查找就行了,注意边界. class Solution { public: i ...
随机推荐
- ngin负载均衡集群(一)
一.nginx负载均衡集群介绍: 1.反向代理与负载均衡概念简介严格地说, nginx仅仅是作为 Nginx Proxy反向代理使用的,因为这个反向代理功能表现的效果是负载均衡集群的效果,所以本文称之 ...
- formpanel布局的学习
FormPanel有两种布局:form和column,form是纵向布局,column为横向布局.默认为后者.使用layout属性定义布局类型.对于一个复杂的布局表单,最重要的是正确分割,分割结果直接 ...
- 第11课 文章分类(组件化开发) Thinkphp5商城第四季
目录 思路: 控制器里 扩展类里: 视图层: 思路: 控制器查出所有数据后调用扩展类里的无限级分类 public function catetree($cateRes) 方法. 把排序好的数据传给视图 ...
- Python学习笔记:math模块(数学),random模块(随机数)
math模块 math模块用于数学意义上的一些计算,常用的方法有: math.pi:PI的值(3.141592653589793). math.floor(x):返回一个小于等于x的最大整数(浮点类型 ...
- 免费证书Let’s Encrypt
我们自己也可以签发 SSL 安全证书,但是我们自己签发的安全证书不会被主流的浏览器信任,所以我们需要被信任的证书授权中心( CA )签发的安全证书.而一般的 SSL 安全证书签发服务都比较贵,比如 G ...
- 安装 ubuntu 后,使用 sed 更换国内源
cd /etc/aptsed -i "s/archive.ubuntu.com/mirrors.aliyun.com/g" /etc/apt/sources.list也可以使用 1 ...
- [Noip2016]换教室(期望+DP)
Description 题目链接:Luogu Solution 这题结合了DP和概率与期望,其实只要稍微知道什么是期望就可以了, 状态的构造很关键,\(F[i][j][0/1]\)表示已经到第\(i\ ...
- python-PIL模块的使用
PIL基本功能介绍 from PIL import Image from PIL import ImageEnhance img = Image.open(r'E:\img\f1.png') img. ...
- 商品评分效果JavaScript
<script> window.onload=function(){ //----------选中的星星会多出一个属性:isClick="true" 藉此来获取评分-- ...
- day 17 jQuery
什么是jQuery? 可以把它认为是python中的模块,导入就可以使用模块中的功能. jQuery 的版本: 1.xx 系列 2.xx 系列 3.xx 系列 最常用的为1 系列,1系列最新版为1.1 ...