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

题意分析:

  本题是在一个递增数组中查找目标值target的下标,如果找不到那么返回target应该插入的下标,使得插入之后依然保持递增。可以得到两个信息:1.数组不会有重复值;2.数组是严格单调递增的。

解答:

  如果看过【LeetCode题意分析&解答】34. Search for a Range这篇解析之后,应当立即想到,本题其实就是一个变种,其实比那道题更为简单。这道题相当于普通的二分查找变了一下条件,和上道题寻找左边界的代码几乎一致。

AC代码:

class Solution(object):
def searchInsert(self, nums, target):
left, right = 0, len(nums) - 1
while left < right:
mid = (left + right) / 2
if nums[mid] < target:
left = mid + 1
else:
right = mid
return left + 1 if nums[left] < target else left

【LeetCode题意分析&解答】35. Search Insert Position的更多相关文章

  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题意分析&解答】40. Combination Sum II

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

  6. 【LeetCode题意分析&解答】37. Sudoku Solver

    Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...

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

  8. 35. Search Insert Position@python

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

  9. 【LeetCode题意分析&解答】34. Search for a Range

    Given a sorted array of integers, find the starting and ending position of a given target value. You ...

随机推荐

  1. node.js(八) 有趣的东西才开始哦

    ### Express介绍 npm提供了大量的第三方模块,其中不乏许多Web框架,比如我们本章节要讲述的一个轻量级的Web框架 ——— Express. Express是一个简洁.灵活的node.js ...

  2. iOS7、iOS8推送通知的区别

    iOS8版本以后的推送通知代码[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificati ...

  3. redis memcache

    谈谈Memcached与Redis(一) 1. Memcached简介 Memcached是以LiveJurnal旗下Danga Interactive公司的Bard Fitzpatric为首开发的高 ...

  4. Docker镜像与仓库(一)

    Docker镜像与仓库(一) Docker镜像与仓库(一) 如何查找镜像? Docker Hub https://registry.hub.docker.com docker search [OPTI ...

  5. struts2笔记03-ActionContext

    1.概念 ActionContext是action的上下文,它包括action执行所需要的对象.struts2对每一个action都会创建一个新的ActionContext实例,同Action一样,是 ...

  6. linux下/proc/sysrq-trigger文件的功能

    /proc/sysrq-trigger该文件能做些什么事情呢? # 立即重新启动计算机 (Reboots the kernel without first unmounting file system ...

  7. jQuery中find()和filter()的区别

    filter(expr):筛选出与指定表达式匹配的元素的元素集合,其中expr可以是多个选择器的组合.是对自身集合元素的筛选. find()会在元素内寻找匹配元素,而filter()是筛选元素:是对它 ...

  8. Unix/Linux环境C编程入门教程(11) 开发环境搭建VMWare虚拟安装之虚拟化检测

    常开启虚拟化技术.如果你的主板是最新的,它在冷启动后能够检测到配置变化.每当我改变我的主板上的VT设置,它都会自动推迟下次重新启动生效.如何确定VT已经开启或禁用? 如图表示成功开启了VT. F10 ...

  9. C语言入门(3)——对Hello World程序的解释

    上篇我们写了一个最简单的程序.这个简单的程序包含了很多重要的内容.本篇我们通过这个最简单的Hello World程序逐一讲解C语言程序的一些特点. 打开Visual Studio 2013 通过菜单- ...

  10. NOI2014 Day1

    NOI2014 Day1 起床困难综合症 题目描述:给出\(n\)个操作,每个操作包含一种运算\((XOR.OR.AND)\)和一个数,已知一开始的数字为\([0, m]\),问经过\(n\)次操作后 ...