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. 求解方程A5+B5+C5+D5+E5=F5

    方程A5+B5+C5+D5+E5=F5刚好有一个满足0<A≤B≤C≤D≤E≤F≤75的整数解.请编写一个求出该解的程序: using System; namespace ReverseTheEx ...

  2. HDU3440(差分约束)

    House Man Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  3. django项目一 登录注册

    STATIC_URL = '/static/' STATICFILES_DIRS = [ os.path.join(BASE_DIR,'static') ] AUTH_USER_MODEL = 'cr ...

  4. Java 基础知识总结 3

    13.java类集 类集实际上是一个动态的对象数组,与一般的对象数组不同,类集中的对象内容可以任意扩充. 类集的特征: 1)这种框架是高性能的 2)框架必须允许不同类型的类集以相同的方式和高度互操作方 ...

  5. error LNK2001: 无法解析的外部符号 _H5T_NATIVE_DOUBLE_g

    最近在编译一个C++动态链接库时遇到一个奇怪的问题,我们基于GsTL实现了GIS地统计分析中的半变异函数分析以及 克吕格插值,GsTL在计算半变异函数时依赖HDF5库,当添加了HDF5的头文件.lib ...

  6. 禅道项目管理软件 为提交Bug页面添加“优先级”字段

    为提交Bug页面添加“优先级”字段 by:授客 QQ:1033553122 测试环境: 禅道项目管理软件7.1.stable版本 备注:仅适合windows版本,linux下,直接在页面管理后台安装官 ...

  7. Python+Selenium笔记(四):unittest的Test Suite(测试套件)

    (一) Test Suite测试套件 一个测试套件是多个测试或测试用例的集合,是针对被测程序的对应的功能和模块创建的一组测试,一个测试套件内的测试用例将一起执行. 应用unittest的TestSui ...

  8. Python 数据分析基础小结

    一.数据读取 1.读写数据库数据 读取函数: pandas.read_sql_table(table_name, con, schema=None, index_col=None, coerce_fl ...

  9. LeetCode题解之Find All Numbers Disappeared in an Array

    1.题目描述 2.问题分析 使的 A[i] = i+1 ,最后检查不满足这个条件的i+1 .即为缺失的值. 3.代码 vector<int> findDisappearedNumbers( ...

  10. LeetCode题解之Rotate String

    1.题目描述 2.问题分析 直接旋转字符串A,然后做比较即可. 3.代码 bool rotateString(string A, string B) { if( A.size() != B.size( ...