美文网首页
leetcode35. 搜索插入位置

leetcode35. 搜索插入位置

作者: 冰源 | 来源:发表于2018-10-18 11:20 被阅读11次
搜索插入位置
class Solution:
    def searchInsert(self, nums, target):
        """
        :type nums: List[int]
        :type target: int
        :rtype: int
        """
        
        left = 0
        right = len(nums)-1
        mid = (left+right)//2
        if target<nums[left]:return 0
        elif target>nums[right]: return len(nums)
        while left<=right:
            if target==nums[mid]:return mid
            elif target>nums[mid] and target<=nums[mid+1]:return mid+1
            elif target<nums[mid]:right=mid
            else:left=mid+1
            mid = (left+right)//2

Runtime: 36 ms, faster than 98.77% of Python3 online submissions for Search Insert Position.

相关文章

  • leetcode35. 搜索插入位置

    Runtime: 36 ms, faster than 98.77% of Python3 online subm...

  • LeetCode35.搜索插入位置 JavaScript

    给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引。如果目标值不存在于数组中,返回它将会被按顺序插入的...

  • 搜索插入位置

    给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引。如果目标值不存在于数组中,返回它将会被按顺序插入的...

  • 搜索插入位置

    题目描述 给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引。如果目标值不存在于数组中,返回它将会被按...

  • 搜索插入位置

    给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引。如果目标值不存在于数组中,返回它将会被按顺序插入的...

  • 搜索插入位置

  • 搜索插入位置

    搜索插入位置 给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引。如果目标值不存在于数组中,返回它将会...

  • 搜索插入位置

    for i in range(len(nums)): if target <= nums[i]: ...

  • 搜索插入位置

    题目需求 给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引。如果目标值不存在于数组中,返回它将会被按...

  • 搜索插入位置

    给定一个排序数组和一个目标值,如果在数组中找到目标值则返回索引。如果没有,返回到它将会被按顺序插入的位置。 你可以...

网友评论

      本文标题:leetcode35. 搜索插入位置

      本文链接:https://www.haomeiwen.com/subject/qiujzftx.html