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

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

 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. 使用yum快速升级CentOS 6.5内核到 3.10.28

    网上有不少升级CentOS内核的文章,如<CentOS 6.5 升级内核到 3.10.28>,大部分都是下载源码编译,有点麻烦. 在yum的ELRepo源中,有mainline(3.13. ...

  2. FZYZ-2071 A Simple Math Problem IX

    P2071 -- A Simple Math Problem IX 时间限制:1000MS      内存限制:262144KB 状态:Accepted      标签:    数学问题-博弈论    ...

  3. HDOJ(HDU) 2139 Calculate the formula(水题,又一个用JavaAC不了的题目)

    Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) 看到这个时间,我懵逼了... 果然,J ...

  4. lightoj 1251 (Two_Sat)

    #include<cstdio> #include<cstring> #include<iostream> #include<cmath> #inclu ...

  5. windows Compiler toolchain env

    1,gygwin

  6. 【纯干货】SVN使用时应注意的那些事

    一.SVN使用步骤 检出 checkout 更新 update 冲突 confilicte 添加 Add (没有添加项目可不写) 填写svn日志 提交 commit你以为到这儿就结束了吗?....NO ...

  7. oracle 大文本由clob来存

    file字段是varchar2类型(最多只能存储4000字符),空间不够用了,因此将其改为clob类型(支持4G存储量). 但是如果该字段中已经有数据,是不为空的,直接用语句改,会报错. 那么需要借助 ...

  8. LibreOffice连接orcle 11g

    1.安装java 2.安装JDBC 官网下载 复制到/usr/java/jdk1.8.0_111/jre/lib/ext文件下

  9. ajax同步刷新

    \js\ajax.js //图片形式状态切换 function toggle(url,action,obj,str,id){//状态转换/obj-当前对象/str-传入类型字符串/id-传入ID $. ...

  10. Qt 学习之路 :Qt 绘制系统简介

    Qt 的绘图系统允许使用相同的 API 在屏幕和其它打印设备上进行绘制.整个绘图系统基于QPainter,QPainterDevice和QPaintEngine三个类. QPainter用来执行绘制的 ...