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

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

 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. 输出password

  2. matlab制造一个64*64的仿真数据

    fid = fopen('test_001.img','w'); r=random('Normal',100,0,64,64); z=random('Uniform',0,5,64,64); %%%% ...

  3. SQL时间格式化

    1 取值后格式化 {:d}小型:如2005 {:D}大型:如2005年5月6日 {:f}完整型 2 当前时间获取 DateTime.Now.ToShortDateString 3 取值中格式化SQL ...

  4. CodeForces 595B

    题目链接: http://codeforces.com/problemset/problem/595/B 题意: 有一个n位的电话号码,每位电话号码将分成n/k段,每段均为k个数,求出满足以下要求的电 ...

  5. 怎么用copy关键字

    出题者简介: 孙源(sunnyxx),目前就职于百度 整理者简介:陈奕龙,目前就职于滴滴出行. 转载者:豆电雨(starain)微信:doudianyu 用途: NSString.NSArray.NS ...

  6. redis学习心得之二【redis主从配置】

    在前一节我们已经实践启动了一个redis服务,我们将其作为主机,现为其创建一个从机作备份使用 1.复制一份配置出来为从机所用 ~$ cp       redis/etc/redis.conf     ...

  7. Google Guava官方教程(中文版)地址

    Google Guava官方教程(中文版) http://ifeve.com/google-guava/ 瓜娃啊瓜娃

  8. maven建module子模块

    在父工程中,点击new ->other  ->maven  -> maven module, 按照提示直到完成. module 可以是普通的工程也可以是web工程. 遇到的问题: 新 ...

  9. Linux下生产者与消费者的线程实现

    代码见<现代操作系统> 第3版. 为了显示效果,添加了printf()函数来显示运行效果 #include<stdio.h> #include<pthread.h> ...

  10. [转] linux下的c/c++调试器gdb

    PS:1. 断点C++类函数,用b 命名空间::类名::方法名 2. 编译参数一定要加-g,才可断点调试 http://www.cnblogs.com/xd502djj/archive/2012/08 ...