leetcode35-search insert position
来源:互联网 发布:大数据需要掌握的技术 编辑:程序博客网 时间:2024/05/22 15:26
1、渣渣渣法一:耗时226ms,beats 0.02%
class Solution(object): def searchInsert(self, nums, target): """ :type nums: List[int] :type target: int :rtype: int """ if nums == []: return 0 n = len(nums) if n ==1 : if target <= nums[0]: return 0 else: return 1 for i in range(n-1): if target in nums: if nums[i] == target: return i if nums[-1] == target: return n-1 else: if nums[i] < target and nums[i+1]> target: return i+1 if target < nums[0]: return 0 if target > nums[-1]: return n
2、二叉树
def searchInsert(self, nums, target): l , r = 0, len(nums)-1 while l <= r: mid=(l+r)/2 if nums[mid]== target: return mid if nums[mid] < target: l = mid+1 else: r = mid-1 return l
3、获取原数列中比target小的所有数
class Solution(object): def searchInsert(self, nums, target): """ :type nums: List[int] :type target: int :rtype: int """ return len([x for x in nums if x<target])
阅读全文
0 0
- [LeetCode35]Search Insert Position
- Leetcode35 Search Insert Position
- [LeetCode35]Search Insert Position
- Leetcode35: Search Insert Position
- Leetcode35. Search Insert Position
- leetcode35: Search Insert Position
- Leetcode35. Search Insert Position
- LeetCode35. Search Insert Position
- leetcode35~Search Insert Position
- leetcode35. Search Insert Position
- leetcode35-search insert position
- LeetCode35. Search Insert Position
- Leetcode35.Search Insert Position
- leetcode35. Search Insert Position
- LeetCode35——Search Insert Position
- LeetCode35. Search Insert Position轻松解决
- 【LeetCode35】【Search Insert Position】二分法查找
- LeetCode: Search Insert Position
- ScrollView嵌套recyclerView问题
- 硬盘分区丢失的文件数据怎么恢复?
- 2、百分比
- 牛客剑指offer刷题记录(三)
- ConcurrentHashMap 源码解读
- leetcode35-search insert position
- 欢迎使用CSDN-markdown编辑器
- Material Design-Surface平面第二篇
- 网络基础(一)
- 解决git提交问题error: The requested URL returned error: 403 Forbidden while accessing
- 阿里云服务器linux(centos)常用命令
- mongodb--java连接数据库实现增删改查
- 【Linux】学会 core dump 事后调试 快速定位段错误
- Maven编译报错[ERROR] Fatal Error: Unable to find package java.lang in classpath or bootclasspath