题目意思:在递增数组中找到目标数的位置,如果目标数不在数组中,返回其应该在的位置。

思路:折半查找,和相邻数比较,注意边界

 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)的更多相关文章

  1. 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导致的溢 ...

  2. [Leetcode][Python]35: Search Insert Position

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 35: Search Insert Positionhttps://oj.le ...

  3. [array] leetcode - 35. Search Insert Position - Easy

    leetcode - 35. Search Insert Position - Easy descrition Given a sorted array and a target value, ret ...

  4. LeetCode练题——35. Search Insert Position

    1.题目 35. Search Insert Position Easy 1781214Add to ListShare Given a sorted array and a target value ...

  5. 【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 ...

  6. 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 ...

  7. 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 ...

  8. [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 ...

  9. [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 ...

随机推荐

  1. 【最短路】NEERC15 F Froggy Ford(2015-2016 ACM-ICPC)(Codeforces GYM 100851)

    题目链接: http://codeforces.com/gym/100851 题目大意: 一只青蛙跳过宽为W的河,河中游N个石头,坐标xi,yi,现在往河中间添加一个石头,使得每次跳跃的最大的距离最小 ...

  2. GeoPandas官方中文文档--译著

    译自GeoPandas 0.1.0 文档(原版译著,有错误欢迎交流,转载请注明) GeoPandas是一个开源项目,它的目的是使得在Python下更方便的处理地理空间数据.GeoPandas扩展了pa ...

  3. 在Ubuntu中安装Redis

    原文地址:http://blog.fens.me/linux-redis-install/ 在Ubuntu中安装Redis R利剑NoSQL系列文章,主要介绍通过R语言连接使用nosql数据库.涉及的 ...

  4. selenium webdriver python 操作Chrome浏览器

    Step1: 下载chromedriver. 下载路径: http://chromedriver.storage.googleapis.com/index.html 选择一个合适的下载即可.我下载的是 ...

  5. sql server 常用小知识点

    1. sql server的语法:中文要加 N select * from eVA_EMPBoard where name = N'施纪平' 而oracle的不用 2.

  6. Django之牛刀初试

    一.Django安装 1.使用pip安装django pip3 inistall django 2.将django-admin加入到环境变量中 # 如果是windows的系统需要操作这一步,linux ...

  7. CSU1661: Query Mutiple

    Description One day,Little-Y saw many numbers standing in a row. A question suddenly appeared in her ...

  8. 让你的WizFi250适应各种气候

    这篇文章会具体描写叙述如何马上得到指定城市的天气状况(比方首尔).由OpenWeatherMap提供. 用JSON(由OpenWeatherMap提供),XML和一个以太网模块.使WIZnet-Wiz ...

  9. [转] Java多线程发展简史

    这篇文章,大部分内容,是周五我做的一个关于如何进行Java多线程编程的Knowledge Sharing的一个整理,我希望能对Java从第一个版本开始,在多线程编程方面的大事件和发展脉络有一个描述,并 ...

  10. [转] STL源码学习----lower_bound和upper_bound算法

    http://www.cnblogs.com/cobbliu/archive/2012/05/21/2512249.html PS: lower_bound of value 就是最后一个 < ...