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. mantis 中文统计报表乱码问题解决办法

    mantis 中文报表乱码问题 1.安装mantisTB 1.2.17:a.安装插件:管理-->插件管理-->安装MantisGraph(Mantis图表 1.0) 插件b.修改配置文件: ...

  2. 无法下载图片 App Transport Security has blocked a cleartext HTTP (http://) resource load since it is insecure. Temporary exceptions can be configured via your app's Info.plist file

    刚学线程通信,提示: 2016-01-27 11:11:02.246 20-9 gcd3 communicationOfThread[5193:298643] App Transport Securi ...

  3. C++虚函数在内存中的实现

    首先来一张图,一目了然: 然后把相应的代码贴上来: class A { int a; public: virtual void f(); virtual void g(int); virtual vo ...

  4. codeforces 15D . Map 优先队列

    题目链接 题目意思很简单nm的矩阵里, 选若干个ab的小矩阵, 定义每个矩阵的值为这个矩阵里的所有数的和-最小值*数的个数. 选小矩阵时, 优先选值最小的,然后次小的.. 知道不能选位置. 输出所有矩 ...

  5. 给进程分配cpu核心

    新负责的程序采用生产者和消费者的模式,生产者的速度非常快,数据几乎都在内存里,处理起来很快.而消费者要频繁的I/O.所以打算给生产者和消费者分配不一样的核心. 生产者只需要一个核心就够了,其余分配给消 ...

  6. 初识Sencha Touch:面板Panel

    HTML代码: <!doctype html> <html> <head> <meta charset="utf-8"> <t ...

  7. sql CAST用法

    (1).CAST()函数的参数是一个表达式,它包括用AS关键字分隔的源值和目标数据类型.以下例子用于将文本字符串'12'转换为整型: ' AS int) (2).返回值是整型值12.如果试图将一个代表 ...

  8. U盘常见故障及检修

    一般U盘故障分为软故障和硬故障,其中以软故障最为常见.  软故障主要是指U盘有坏块,从而导致U盘能被计算机识别,但没有盘符出现,或者有盘符出现,但当打开U盘时却提示要进行格式化,而格式化又不能成功.前 ...

  9. Linux-storage-stack-diagram

    just a diagram 一目了然. 对于isci 只是用过LIO和STGT 两种后端. 这里有各种后端的比较. http://scst.sourceforge.net/comparison.ht ...

  10. oracle job 定时执行 存储过程

    oracle job 定时执行 存储过程   一:简单测试job的创建过程案例: 1,先创建一张JOB_TEST表,字段为a 日期格式 SQL> create table JOB_TEST(a ...