题目来源

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. CodeForces Round 194 Div2

    A. Candy Bagstime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputs ...

  2. strerror

    #include<stdio.h> #include<string.h> #include<errno.h> void main(void ) { printf(& ...

  3. HTML中为何P标签内不可包含块元素?

    起因:在做项目时发现原本在DW中无误的代码到了MyEclipse6.0里面却提示N多错误,甚是诧异.于是究其原因,发现块级元素P内是不能嵌套DIV的. 深究:我们先来认识in-line内联元素和blo ...

  4. HDU 1542 Atlantis(线段树扫描线+离散化求面积的并)

    Atlantis Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total S ...

  5. PHP+jQuery 列表分页类 ( 支持 url 分页 / ajax 分页 )

    /* ******* 环境:Apache2.2.8 ( 2.2.17 ) + PHP5.2.6 ( 5.3.3 ) + MySQL5.0.51b ( 5.5.8 ) + jQuery-1.8.3.mi ...

  6. ThinkPHP中Xheditor编辑器报错

    在使用Xheditor这个插件时..我按照他里面的domo中的方法放到自己的控制器中..出现如下问题 以下是我节选的错误提示 /index.php/admin/goods/upload/ 上传接口发生 ...

  7. PHP测试用例练习

    本测试用例是一个判断三角形类型的练习测试用例,基于Netbeans 8.1IDE环境,和phpunit-5.2.10以及脚手架工具phpunit-skelgen-2.0.1.具体的环境搭建可参照: h ...

  8. PHP文件操作 之读取目录信息

    //定义一个函数 读取目录信息的函数 function dirInfo($dirName) { //判断目录是否存在 if (!file_exists($dirName)) { die('目录不存在! ...

  9. MySQL并发调优和IO调优

    一.myisam的IO调优1.myisam通常在每次写入后把索引的改变刷写到磁盘上.所以批处理通常会更快点.做到这点,可以通过LOCK TABLES,他可以把写入控制到对表解锁.还可以用delay_k ...

  10. SET GLOBAL slow_query_log=1

    - Variable 'slow_query_log' is a GLOBAL variable and should be set with SET GLOBAL SHOW VARIABLES LI ...