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

原题地址: Search Insert Position

思路:

二分查找

代码:

class Solution(object):
def searchInsert(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: int
"""
l, r = 0, len(nums)-1
while l <= r:
mid = (l + r) / 2
if nums[mid] >= target:
r = mid - 1
else:
l = mid + 1
return l

时间复杂度: O(log(n))

空间复杂度: O(1)

35. Search Insert Position@python的更多相关文章

  1. [Leetcode][Python]35: Search Insert Position

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 35: Search Insert Positionhttps://oj.le ...

  2. [array] leetcode - 35. Search Insert Position - Easy

    leetcode - 35. Search Insert Position - Easy descrition Given a sorted array and a target value, ret ...

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

  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. 【LeetCode】35. Search Insert Position 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 二分查找 日期 题目地址:https://leetc ...

  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

    1 题目: Given a sorted array and a target value, return the index if the target is found. If not, retu ...

  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. AcDbDictionary of AcDbDatabase

    GroupDictionary MLStyleDictionary LayoutDictionary PlotStyleNameDictionary MaterialDictionary Visual ...

  2. 我的省选 Day -15

    Day  -15 23:22:45 还有十几天就要去参加省选啦~,今天开始写日记记录一下,所以今天是第负十五天. 今天是阶段考的第二天(由于奥赛不用考试的我7:30才慢悠悠地到机房 早上学习动态DP, ...

  3. Jenkins+Git+Docker+K8s部署

    准备工作 Jenkins已安装 Docker和K8s部署运行成功 代码管理工具使用Git 最近公司项目使用Jenkins+Git+Docker+K8s进行持续化构建部署,这里笔者整理了一下构建部署的相 ...

  4. Java 多线程高并发编程 笔记(一)

    本篇文章主要是总结Java多线程/高并发编程的知识点,由浅入深,仅作自己的学习笔记,部分侵删. 一 . 基础知识点 1. 进程于线程的概念 2.线程创建的两种方式 注:public void run( ...

  5. 题解 UVA11354 【Bond】

    并查集+按秩合并 传送门 大意:给出一张n个点m条边的无向图, 每条边有一个权值,有q个询问, 每次给出两个点s.t,找一条路, 使得路径上的边的最大权值最小. 我们可以发现,跑最小生成树会跑挂, 那 ...

  6. Netty(4)Stream by codec(粘包与拆包)

    TCP/IP,传输的是byte[],将byte[]放入队列中.可能会发生粘包和拆包. 比如,客户端向服务端发送了2条消息,分别为D1,D2,可能产生的情况,如下图所示: 情况一:正常的. 情况二:粘包 ...

  7. java中存储金额

    很早之前, 记得一次面试, 面试官问存储金钱用什么数据类型? 当时只知道8种数据类型(boolean, byte, short, int, long, float, double, char)的我, ...

  8. TVS选型

    与保护器件并联使用的电压型保护器件,速度快,冲击脉冲功率高.正常高阻抗,超过击穿电压后导通将两端电压钳位在VC(即被保护的电压级别) 命名规则SMAJ/BJ/CJ/DJ分别表示不同的脉冲功率为400W ...

  9. 【干货分享】大话团队的GIT分支策略进化史

    封面 作为一名85后的技术男,一转眼10年过去了(一不小心暴露了年龄,虽然我叫18岁fantasy),亲手写代码已经是5年前了,目前主要负责公司的软件产品的规划和设计(所以最近写的东西也主要与设计和产 ...

  10. php 根据周数获取当周的开始日期与最后日期

    /** * 根据第几周获取当周的开始日期与最后日期 * @param int $year 年份 $weeks = get_week($year) * @param 如获取第18周的开始日期$weeks ...