题目描述

给出一个有序的数组和一个目标值,如果数组中存在该目标值,则返回该目标值的下标。如果数组中不存在该目标值,则返回如果将该目标值插入这个数组应该插入的位置的下标
假设数组中没有重复项。
下面给出几个样例:
[1,3,5,6], 5 → 2
[1,3,5,6], 2 → 1
[1,3,5,6], 7 → 4
[1,3,5,6], 0 → 0

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.

Here are few examples.
[1,3,5,6], 5 → 2
[1,3,5,6], 2 → 1
[1,3,5,6], 7 → 4
[1,3,5,6], 0 → 0

示例1

输入

复制

[1,3,5,6],5

输出

复制

2
class Solution {
public:
    /**
     *
     * @param A int整型一维数组
     * @param n int A数组长度
     * @param target int整型
     * @return int整型
     */
    int searchInsert(int* A, int n, int target) {
        // write code here
        if (n==0) return 0;
        int l=0,h=n,m;
        while (l<h){
            m=l+((h-l)>>1);
            if (A[m] >target){
                h=m;
            }
            else if (A[m]<target){
                l=m+1;
            }
            else {
                return m;
            }
            
        }
        if (target<A[0])return 0;
        if (target>A[n-1]) return n;
        return h;
    }
};
#
#
# @param A int整型一维数组
# @param target int整型
# @return int整型
#
class Solution:
    def searchInsert(self , A , target ):
        # write code here
        if not A:
            return 0
        if target in A:
            return A.index(target)
        else :
            for i in range(len(A)):
                if A[i]>target:
                    return i
            return len(A)
               

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

  10. 60. Search Insert Position 【easy】

    60. Search Insert Position [easy] Given a sorted array and a target value, return the index if the t ...

随机推荐

  1. 【题解】Tree

    题目戳我 \(\text{Solution:}\) 考虑点分治.对于这个两点之间,它意味着这点对必须是不一样的. 考虑用双指针统计答案.显然,对于两个数\(a,b\),要让\(a+b=k,a\)越大则 ...

  2. Go path/filepath包

    path/filepath 标准库path中有的功能filepath全部具备, 所以使用filepath即可. isABS() 判断一个路径是不是绝对路径. package main import ( ...

  3. Pock 把 Touch Bar 变成系统中的 Dock 栏

    Pock 把 Touch Bar 变成系统中的 Dock 栏 Pock 是一款 macOS App,你可以通过它把 Touch Bar 变成系统中的 Dock 栏,直接用来切换和启动 App,尽享全屏 ...

  4. Centos7安装Java8

    centos7 用yum安装java8  

  5. fastadmin 增加批量操作字段 提示无权限

    是这样的找了权限节点的问题,始终找不到,后来 在社区传世人回答别人问题是提及到  $multiFields 就全局搜了下 在基类 Backend 里找到了这个.然后拿到 控制器中添加需要的参数 再次尝 ...

  6. golang 进行grpc调用

    参考https://blog.csdn.net/qq_32744005/article/details/105606383 go get google.golang.org/grpc go get - ...

  7. linux系统上用户态pppoe收发包过程

    花了几天看了一下ppp/pppoe有关的东西,画了一下用户态pppoe收发包的示意图.

  8. 多测师讲解接口自动化测试 _requests_高级讲师肖sir

    rep=requests.post 错误方法: 1.在代理中---把高级中----代理-----去除勾选,调用失败

  9. BigInteger和BigDecimal

    BigInteger BigInteger的作用是对整数做计算,一般的使用String类型初始化BigInteger,它除了封装了基本的加减乘除运算外还提供了signum(),abs()等函数,使用方 ...

  10. oh my zsh 安装

    date: "2020-10-18T12:36:00+08:00" title: "oh my zsh 安装" tags: ["zsh",& ...