35 Search Insert Position(找到数的位置Medium)
题目意思:在递增数组中找到目标数的位置,如果目标数不在数组中,返回其应该在的位置。
思路:折半查找,和相邻数比较,注意边界
class Solution {
public:
int searchInsert(vector<int>& nums, int target) {
int start=,end=nums.size()-;
int flag=;
while(start<=end){
flag=(start+end)/;
if(nums[flag]==target)return flag;
else if(nums[flag]<target){
if(flag==nums.size()-||nums[flag+]>=target)return flag+; //和相邻数比较
else start=flag+;
}
else{
if(flag==||nums[flag-]<target)return flag;
else end=flag-;
}
}
}
};
35 Search Insert Position(找到数的位置Medium)的更多相关文章
- leetcode 704. Binary Search 、35. Search Insert Position 、278. First Bad Version
704. Binary Search 1.使用start+1 < end,这样保证最后剩两个数 2.mid = start + (end - start)/2,这样避免接近max-int导致的溢 ...
- [Leetcode][Python]35: Search Insert Position
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 35: Search Insert Positionhttps://oj.le ...
- [array] leetcode - 35. Search Insert Position - Easy
leetcode - 35. Search Insert Position - Easy descrition Given a sorted array and a target value, ret ...
- LeetCode练题——35. Search Insert Position
1.题目 35. Search Insert Position Easy 1781214Add to ListShare Given a sorted array and a target value ...
- 【LeetCode】35. Search Insert Position (2 solutions)
Search Insert Position Given a sorted array and a target value, return the index if the target is fo ...
- 35. Search Insert Position@python
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
- LeetCode 35. Search Insert Position (搜索嵌入的位置)
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
- [LeetCode] 35. Search Insert Position 搜索插入位置
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
- [LeetCode] 35. Search Insert Position 解决思路
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
随机推荐
- kafka 消息服务
apache kafka参考 http://kafka.apache.org/documentation.html 消息队列方式: 点对点: 消息生产者生产消息发送到queue中,然后消息消费者从qu ...
- Java系统变量设置方式
近期碰到一个编码的问题,发现整个平台都是用的GB2312,因此导致webservice调用时有些字不能正常接受. 反编译中间件的源码如下: public static final String nod ...
- 使用Array
public class UsingArray { public static void output(int[]Array) { if(Array!=null) ...
- 学习 opencv---(10)形态学图像处理(2):开运算,闭运算,形态学梯度,顶帽,黒帽合辑
上篇文章中,我们重点了解了腐蚀和膨胀这两种最基本的形态学操作,而运用这两个基本操作,我们可以实现更高级的形态学变换. 所以,本文的主角是OpenCV中的morphologyEx函数,它利用基本的膨胀和 ...
- python中如何判断list中是否包含某个元素
在python中可以通过in和not in关键字来判读一个list中是否包含一个元素 pythontab = ['p','y','t','h','o','n','t','a','b'] if 't' ...
- NOI2015 程序自动分析 prog
何等水题 某神犇仿关押罪犯的写法 却写挂了 然而实际上并不需要补集之类的 #include<iostream> #include<cstring> #include<c ...
- SEO为什么越来越难?
不是很长的折腾seo该,无脑想如何很长的头发外链,一方面,没有花费的时间SEO在,另一方面SEO越来越难,的另一个方面,也SEO特征,不可控和不可预测性,致使我们花了非常多的情况下,SEO大部分时间都 ...
- 浅析mysql 共享表空间与独享表空间以及他们之间的转化
innodb这种引擎,与MYISAM引擎的区别很大.特别是它的数据存储格式等.对于innodb的数据结构,首先要解决两个概念性的问题: 共享表空间以及独占表空间.什么是共享表空间和独占表空间共 ...
- Valgrind简介:
Valgrind是动态分析工具的框架.有很多Valgrind工具可以自动的检测许多内存管理和多进程/线程的bugs,在细节上剖析你的程序.你也可以利用Valgrind框架来实现自己的工具. Valgr ...
- ADO.NET基础
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.W ...