【Python】搜索插入位置
2021/4/13 1:25:46
本文主要是介绍【Python】搜索插入位置,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
目录
- 导航:
- 代码:
- 哔哔一句:
导航:
自己:https://sleepymonster.cn/
仓库:https://github.com/hengyi666
代码:
class Solution: def SearchInsert(self, nums: list, target: int) -> int: if target <= nums[0]: return 0 if target >= nums[-1]: return len(nums) i, j = 0, len(nums) - 1 while i < j: mid = (i+j)//2 if target <= nums[mid]: j = mid else: i = mid + 1 return j nums = [1,3,5,6] target = 2 test = Solution().SearchInsert(nums,target) print(test)
哔哔一句:
一起进步,若有帮助,点个赞吧QAQ
这篇关于【Python】搜索插入位置的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-05-08有遇到过吗?同样的规则 Excel 中 比Python 结果大
- 2024-03-30开始python成长之路
- 2024-03-29python optparse
- 2024-03-29python map 函数
- 2024-03-20invalid format specifier python
- 2024-03-18pool.map python
- 2024-03-18threads in python
- 2024-03-14python Ai 应用开发基础训练,字符串,字典,文件
- 2024-03-13id3 algorithm python
- 2024-03-13sum array elements python