给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引。如果目标值不存在于数组中,返回它将会被按顺序插入的位置。

你可以假设数组中无重复元素。

示例 1:

输入: [1,3,5,6], 5
输出: 2

示例 2:

输入: [1,3,5,6], 2
输出: 1

示例 3:

输入: [1,3,5,6], 7
输出: 4

示例 4:

输入: [1,3,5,6], 0
输出: 0

思路

  • python的list是有index()方法的,如果目标值在数组中就很简单
  • 如果没有的话就从头线性扫描一遍,当然更好的做法是二分找位置

代码

class Solution:
def searchInsert(self, nums: List[int], target: int) -> int:
try:
index_value = nums.index(target)
return index_value
except:
length = len(nums)
for i in range(length):
if nums[i] > target and i > 0:
return i
if nums[i] > target and i == 0:
return 0
if nums[i] < target and i == length-1:
return length # 二分法
class Solution:
def searchInsert(self, nums: List[int], target: int) -> int:
length = len(nums)
if length == 0:
return 0
left = 0
right = length while left < right:
mid = (left + right) // 2
if nums[mid] < target: # 此时已经排除mid位置是答案的可能性
left = mid + 1
else:
right = mid
return left

精选题解里有教二分法模板,说的很棒,强烈推荐


注明

来源:力扣(LeetCode)

链接:https://leetcode-cn.com/problems/search-insert-position

Leetcode 35.搜索插入位置 By Python的更多相关文章

  1. Java实现 LeetCode 35 搜索插入位置

    35. 搜索插入位置 给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引.如果目标值不存在于数组中,返回它将会被按顺序插入的位置. 你可以假设数组中无重复元素. 示例 1: 输入: [1, ...

  2. [leetcode] 35. 搜索插入位置(Java)(二分)

    35. 搜索插入位置 二分,太简单,没啥好说的 class Solution { public int searchInsert(int[] nums, int target) { if (nums. ...

  3. [LeetCode]35.搜索插入位置(Java)

    原题地址: search-insert-position 题目描述: 给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引.如果目标值不存在于数组中,返回它将会被按顺序插入的位置. 请必须使 ...

  4. Leetcode之二分法专题-35. 搜索插入位置(Search Insert Position)

    Leetcode之二分法专题-35. 搜索插入位置(Search Insert Position) 给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引.如果目标值不存在于数组中,返回它将会 ...

  5. leetcode笔记——35.搜索插入位置 - CrowFea

    0.问题描述 给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引.如果目标值不存在于数组中,返回它将会被按顺序插入的位置. 你可以假设数组中无重复元素. 示例 1: 12 输入: [1,3 ...

  6. 力扣(LeetCode) 35. 搜索插入位置

    给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引.如果目标值不存在于数组中,返回它将会被按顺序插入的位置. 你可以假设数组中无重复元素. 示例 1: 输入: [1,3,5,6], 5 输 ...

  7. Leetcode题库——35.搜索插入位置

    @author: ZZQ @software: PyCharm @file: searchInsert.py @time: 2018/11/07 19:20 要求:给定一个排序数组和一个目标值,在数组 ...

  8. 【leetcode算法-简单】35. 搜索插入位置

    [题目描述] 给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引.如果目标值不存在于数组中,返回它将会被按顺序插入的位置. 你可以假设数组中无重复元素. 示例 1: 输入: [1,3,5, ...

  9. Leecode刷题之旅-C语言/python-35.搜索插入位置

    /* * @lc app=leetcode.cn id=35 lang=c * * [35] 搜索插入位置 * * https://leetcode-cn.com/problems/search-in ...

随机推荐

  1. CSS3 文字渐变动画

    背景剪裁 语法:background-clip: border-box || padding-box || context-box || no-clip || text 本次用到的就是: -webki ...

  2. 线程Thread、线程池ThreadPool

    Thread调用含参方法,参数必须是object类.ThreadPool调用的方法,无论是否含参,方法必须有object类参数(可不用,但得有) [线程] using System; using Sy ...

  3. Controllers的使用

    代码 angularjs.html <!doctype html> <html> <head> <meta charset="UTF-8" ...

  4. A. Be Positive

    A. Be Positive time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  5. C++入门经典-例4.4-循环嵌套之求n的阶乘

    1:代码如下: // 4.4.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <iostream> using ...

  6. JS基础_Null和Undefind

    1.Null Null类型的值只有一个值,就是null null专门用来表示一个为空的对象 var a=null; console.log(a);//nulltypeof a //object 2.U ...

  7. javascript模块化之CommonJS、AMD、CMD、UMD、ES6

    javascript模块化之CommonJS.AMD.CMD.UMD.ES6 一.总结 一句话总结: CommonJS是同步加载模块,用在服务端:AMD是异步加载模块,用于浏览器端 1.为什么服务器端 ...

  8. JSP——隐式对象(implicit object)

    Servlet容器将几个对象传递给它所运行的Servlet. 例如,在Servlet的service方法中获得HttpServletRequest和HttpServletResponse,并在init ...

  9. ffmpeg循环推流

    ffmpeg循环推流 有时候需要轮播出一路直播 这个时候循环推流就比较方便了 ffmpeg -stream_loop - -re -i d:/Media/a.ts -vcodec h264 -acod ...

  10. jenkins入门-----(1)安装、配置

    Jenkins概念 Jenkins是一个开源的.可扩展的持续集成.交付.部署(软件/代码的编译.打包.部署)的基于web界面的平台.允许持续集成和持续交付项目,无论用的是什么平台,可以处理任何类型的构 ...