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. 介绍Foundation框架

    开始介绍Foundation框架.OC中的Foundation框架是系统提供了,他就相当于是系统的一套api,和Java中的一些系统jar很相似,又早起的一批人开发的,内部有很多现有的类和功能提供给我 ...

  2. C++虚基类的作用

    虚基类的作用     当一个基类被声明为虚基类后,即使它成为了多继承链路上的公共基类,最后的派生类中也只有它的一个备份.例如:class CBase { }:class CDerive1:virtua ...

  3. Linux学习之/etc/init.d/functions详解

    转自:http://blog.chinaunix.net/xmlrpc.php?r=blog/article&uid=28773997&id=3996557 /etc/init.d/f ...

  4. gcc 编译器

    gcc -E test.c  预处理 gcc -v test.c  查看编译的整个过程 objdump -d a.out 查看elf文件

  5. jQuery1.9.1针对checkbox的调整

    在jquery 1.8.x中的版本,我们对于checkbox的选中与不选中操作如下: 判断是否选中 $('#checkbox').prop('checked') 设置选中与不选中状态: $('#che ...

  6. Android GreenDao with Android Studio IDE

    转:http://blog.surecase.eu/using-greendao-with-android-studio-ide/ In this tutorial we will show you ...

  7. project euler 48 Self powers 解决乘法爆long long

    题目链接 求 $ 1^1+2^2+\cdots + 1000^{1000} $ %1e10 的结果. 唯一的坑点是会爆longlong, 所以用特殊的乘法. #include <iostream ...

  8. 介绍几种IC卡

    介绍几种IC卡 Mifare卡:工作频率:13.56MHZ数据保存期:10年 操作时间:96ms读写距离:2.5-10cm   存储容量:8Kbit尺寸:85.6x54x0.76mm 存储器类型:EE ...

  9. Delphi 的接口机制——接口操作的编译器实现过程(1)

    学习COM编程技术也快有半个月了,这期间看了很多资料和别人的程序源码,也尝试了用delphi.C++.C#编写COM程序,个人感觉Delphi是最好上手的.C++的模版生成的代码太过复杂繁琐,大量使用 ...

  10. poj2578---三个数中找出第一个大于168的

    #include <stdio.h> #include <stdlib.h> int main() { int a,b,c; scanf("%d %d %d" ...