class Solution:
# def searchInsert(self, nums, target):
# """
# :type nums: List[int]
# :type target: int
# :rtype: int
# """
# min, pos = 0, 0
# max = len(nums) - 1
# while min <= max:
# # binary search
# pos = (max + min) / 2
# if nums[pos] == target:
# return pos
# elif nums[pos] > target:
# max = pos - 1
# else:
# min = pos + 1
# if min > pos:
# # this means that target is great than pos, and target
# # is not in nums
# return pos + 1
# return pos
def searchInsert(self, nums, target):
l, r = int(0), len(nums) - 1
while l < r:
mid = int((l + r) / 2)
if nums[mid] < target:
l = mid + 1
else:
r = mid
if nums[l] < target:
return l + 1
return l
if __name__ == '__main__':
# begin
s = Solution()
print (s.searchInsert([1,3,5,6],5))
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
python_leetcode面试题解之第35题搜索插入位置_python题解.zip (1个子文件)
python_leetcode面试题解之第35题搜索插入位置_python题解
035_Search_Insert_Position.py 1KB
共 1 条
- 1
资源评论
Ddddddd_158
- 粉丝: 735
- 资源: 206
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功