Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.

You may assume no duplicates in the array.

Example 1:

Input: [1,3,5,6], 5
Output: 2

Example 2:

Input: [1,3,5,6], 2
Output: 1

Example 3:

Input: [1,3,5,6], 7
Output: 4

Example 4:

Input: [1,3,5,6], 0
Output: 0
想法:使用二分查找法,找出target应该在的位置。
class Solution {
public:
    int searchInsert(vector<int>& nums, int target) {
         == nums.size()){
            ;
        }
        ))
            ;
        if(target > nums.back()){
            return nums.size();
        }
        ;
        ;
        ){
            ;
            if(nums.at(mid) == target)
                return mid;
            if(nums.at(mid) < target){
                left = mid;
            }else{
                right = mid;
            }
        }
        return right;
    }
};

leetcode25—Search Insert Position的更多相关文章

  1. LeetCode:Search Insert Position,Search for a Range (二分查找,lower_bound,upper_bound)

    Search Insert Position Given a sorted array and a target value, return the index if the target is fo ...

  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. Leetcode35 Search Insert Position 解题思路(python)

    本人编程小白,如果有写的不对.或者能更完善的地方请个位批评指正! 这个是leetcode的第35题,这道题的tag是数组,python里面叫list,需要用到二分搜索法 35. Search Inse ...

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

  6. leetcode-algorithms-35 Search Insert Position

    leetcode-algorithms-35 Search Insert Position Given a sorted array and a target value, return the in ...

  7. LeetCode: Search Insert Position 解题报告

    Search Insert Position Given a sorted array and a target value, return the index if the target is fo ...

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

  9. Leetcode 二分查找 Search Insert Position

    本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Search Insert Position Total Accepted: 14279 T ...

随机推荐

  1. oracle 中如何查询当前用户可以看到的表名、表对应的所有字段

    前言:利用 oracle 的视图来查询表的相关信息. oracle 查询当前用户下的表名 + 表注释 select t.table_name tableName, f.comments comment ...

  2. nginx配置https转发到tomcat(使用自签名的证书)

    一.使用openSSL生成自签名的证书 1.生成RSA私钥 命令:openssl genrsa -des3 -out server.key 1024 说明:生成rsa私钥,des3算法,1024强度, ...

  3. JNDI 与 LDAP

    对于众多接口服务.协议.互联网名称,总会遇到感到熟悉,但是时间一长就会忘记,所以还是要自己整理一下,加强记忆,当然最好的方式还是动手实践. JNDI : 全称:JAVA NAMING AND Dire ...

  4. js的style.display小问题

    在元素添加class样式隐藏display:none; 使用console.log(xx.style.display);//空值

  5. 自定义适用于手机和平板电脑的 Dynamics 365(二):窗体自定义项

    适用于手机的 Dynamics 365 和 适用于平板电脑的 Dynamics 365 使用窗体作为 Web 应用. 窗体在应用程序中的显示方式为移动体验进行了优化. 下图显示了从 Web 应用程序到 ...

  6. HDFS 2.7.4中hdfs-site.xml参数未配置引发的一些异常

    生产上部署了HDFS 2.7.4,最近遇到了一些异常,记录下来备忘: 一.dfs.datanode.directoryscan.throttle.limit.ms.per.sec DataNode运行 ...

  7. Vue + Spring Boot从零开始搭建个人网站(一) 之 项目前端Vue.js环境搭建

    前言: 最近在考虑搭建个人网站,想了想决定采用前后端分离模式 前端使用Vue,负责接收数据 后端使用Spring Boot,负责提供前端需要的API 就这样开启了我边学习边实践之旅 Vue环境搭建步骤 ...

  8. FileWriter剖析

    集合这种容器存储数据,它只能在内存中临时存储,不能永久存储,这样会导致数据的丢失,所以出现了IO流. IO流用来处理设备之间的数据传输.可以用来做复制文件,上传文件,下载文件. 读数据是输入流,写数据 ...

  9. QQ运动步数&自定义ProgressBar

    效果如下 gif图展示效果不好,实际体验无卡顿 1.自定义属性 早Values目录下New-values resource file,命名为attrs.xml(命名随意,但规范命名为attrs.xml ...

  10. [Android] 修图工具Draw9patch使用小结(附ubuntu快捷截图方法)

    做项目的时候,素材图遇到点问题,然后老大大概给我讲了讲android下面图片格式.9.png和draw 9-patch的用法,感觉很清楚也很有用,所以记录一下. 原文地址请保留http://www.c ...