题目来源

https://leetcode.com/problems/search-insert-position/

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.


题意分析


Input: a sorted list of int

Output: the index if the target is found. If not, return the index where it would be if it were inserted in order.

Conditions:找到target所在的位置或者应该插入的位置

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


题目思路


使用二分法找元素,如果找到直接返回index,如果不能找到,此时又first和last两个值,注意到target总是不小于nums[first],所以比较target和first即可,

  1. 如果target大于nums[first],返回first + 1
  2. 反之返回first

注意到在二分法当中first可能会加1溢出数组,所以在上面步骤当中要在比较前加入判断first是否溢出数组


AC代码(Python)


 _author_ = "YE"
# -*- coding:utf-8 -*- class Solution(object):
def searchInsert(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: int
"""
len1 = len(nums)
first = 0
last = len1
while first < last:
mid = (first + last) // 2
if nums[mid] == target:
return mid
elif nums[mid] < target:
first = mid + 1
else:
last = mid - 1
# print(first,last)
if first < len1 and nums[first] < target:
return first + 1
return first s = Solution()
nums = [1,3]
target = 2
print(s.searchInsert(nums, target))

[LeetCode]题解(python):035-Search Insert Position的更多相关文章

  1. [LeetCode] 035. Search Insert Position (Medium) (C++)

    索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 035. Sea ...

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

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

  3. LeetCode 035 Search Insert Position

    题目要求:Search Insert Position Given a sorted array and a target value, return the index if the target ...

  4. Leetcode 题目整理 Sqrt && Search Insert Position

    Sqrt(x) Implement int sqrt(int x). Compute and return the square root of x. 注:这里的输入输出都是整数说明不会出现 sqrt ...

  5. LeetCode练题——35. Search Insert Position

    1.题目 35. Search Insert Position Easy 1781214Add to ListShare Given a sorted array and a target value ...

  6. LeetCode Arrary Easy 35. Search Insert Position 题解

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

  7. Java for LeetCode 035 Search Insert Position

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

  8. 035 Search Insert Position 搜索插入位置

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

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

  10. LeetCode记录之35——Search Insert Position

    这道题难度较低,没有必要作说明. Given a sorted array and a target value, return the index if the target is found. I ...

随机推荐

  1. BZOJ3833 : [Poi2014]Solar lamps

    首先旋转坐标系,将范围表示成矩形或者射线 如果范围是一条线,则将灯按y坐标排序,y坐标相同的按x坐标排序, 对于y相同的灯,f[i]=min(i,它前面灯发光时刻的第k[i]小值), 线段树维护,$O ...

  2. Designing CSS Layouts With Flexbox Is As Easy As Pie

    This article is an updated excerpt of the chapter “Restyle, Recode, Reimagine With CSS3″ from our Sm ...

  3. 移动端JS 触摸事件基础

    一.手机上的触摸事件   基本事件:   touchstart   //手指刚接触屏幕时触发 touchmove    //手指在屏幕上移动时触发 touchend     //手指从屏幕上移开时触发 ...

  4. Qt make clickable label 制作可点击的Label控件

    Qt中的Label标签控件的作用绝不仅仅限于显示静态文本,其实它的功能很强大,由于其有setPixmap()成员函数,故而可以当显示图片窗口使用,而且还可以实时显示摄像头捕获的图片,然后它对鼠标事件的 ...

  5. JavaScript操作DOM对象

    js的精华即是操作DOM对象 [1]先看代码 <!DOCTYPE html> <html> <head> <meta charset="UTF-8& ...

  6. GridView 下拉搜索

    /** * 下拉筛选 * @column string 字段 * @value mix 字段对应的值,不指定则返回字段数组 * @return mix 返回某个值或者数组 */ public stat ...

  7. HDU 5521 Meeting(虚拟节点+最短路)

    Meeting Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total ...

  8. nmap 高级扫描用法

    nmap提供了四项基本功能(主机发现.端口扫描.服务与版本侦测.OS侦测)及丰富的脚本库.Nmap既能应用于简单的网络信息扫描,也能用在高级.复杂.特定的环境中:例如扫描互联网上大量的主机:绕开防火墙 ...

  9. CodeForces 621C 数学概率期望计算

    昨天训练赛的题..比划了好久才想出来什么意思 之前想的是暴力for循环求出来然后储存数组 后来又想了想 自己萌的可以.. 思路就是求出来每个人与他的右边的人在一起能拿钱的概率(V(或)的关系)然后*2 ...

  10. A trip through the Graphics Pipeline 2011_04

    Welcome back. Last part was about vertex shaders, with some coverage of GPU shader units in general. ...