美文网首页
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. 搜索插入位置

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