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 ...
随机推荐
- Android 开发无线调试
在进行android开发的时候有时候总是连接不上设备,因为设备的USB进行频繁插拔之后会导致一定的损坏.所以在设备已经无法利用数据线进行连接开发工具的时候就需要有那么一个功能,那就是无线调试,是不是感 ...
- 关于TCP的粘包和拆包
问题产生 一个完整的业务可能会被TCP拆分成多个包进行发送,也有可能把多个小的包封装成一个大的数据包发送,这个就是TCP的拆包和封包问题. 下面可以看一张图,是客户端向服务端发送包: 1. 第一种情况 ...
- DFS Zoj 2110
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2110 //2110 #include<stdio.h> #in ...
- cannot be deleted directly via the port API: has device owner network:floatingip
- H - Pots
题目大意: 有一个瓶子A和一个瓶子B,可以有三种操作倒满,倒空,或者把瓶子A倒向瓶子B(或者把瓶子B倒向瓶子A),可以扩展出6种操作,没什么简单的写法,只能一种一种的写..... 当然是使用广搜... ...
- maven打一个可执行的jar包
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-depen ...
- python获取本机IP、mac地址、计算机名
在python中获取ip地址和在php中有很大不同,在php中往往比较简单.那再python中怎么做呢? 我们先来看一下python 获得本机MAC地址: 1 2 3 4 import uuid de ...
- Android Developers:两个视图渐变
淡入淡出动画(也被称为渐隐)逐渐淡出一个UI组件,同时淡入另一个.这个动画在你想在你的应用程序中切换内容或者是视图的情况下非常有用.淡入淡出非常微妙并短,但支持从一个屏幕到下一个屏幕流畅的过渡.当你不 ...
- boost------bind的使用(Boost程序库完全开发指南)读书笔记
bind是c++98标准库中函数适配器bind1st/bind2nd的泛化和增强,可以适配任意的可调用类型,包括函数指针.函数引用.成员函数指针和函数对象. 1.工作原理 bind并不是一个单独的类或 ...
- ZOJ 3822 Domination(概率dp)
一个n行m列的棋盘,每天可以放一个棋子,问要使得棋盘的每行每列都至少有一个棋子 需要的放棋子天数的期望. dp[i][j][k]表示用了k天棋子共能占领棋盘的i行j列的概率. 他的放置策略是,每放一次 ...