【LeetCode】 34. Find First and Last Position of Element in Sorted Array (Python)

样本不均衡时AUC反而下降?用imbalanced-learn库实战解决分类陷阱 本文探讨了样本不均衡时AUC指标下降的现象,并通过imbalanced-learn库实战演示了如何解决这一分类陷阱。文章详细分析了不同采样技术(如SMOTE、ADASYN)对模型性能的影响,并提供了XGBoost和逻辑回归模型的优化建议,帮助数据科学家在实际项目中做出更明智的决策。 阅读详情

【LeetCode】 34. Find First and Last Position of Element in Sorted Array (Python)

题目描述

34. Find First and Last Position of Element in Sorted Array

Medium

Given an array of integers nums sorted in ascending order, find the starting and ending position of a given target value.

Your algorithm’s runtime complexity must be in the order of O(log n).

If the target is not found in the array, return [-1, -1].

Example 1:

Input: nums = [5,7,7,8,8,10], target = 8
Output: [3,4]

Example 2:

Input: nums = [5,7,7,8,8,10], target = 6
Output: [-1,-1]

严谨分析

作为小白的我刷这些,就一个词“懵逼”,这咋返回,要找这个数最多有几个?(我奇怪的关注点)

我学了的查找有:线性、二分、哈希——肯定用二分!我思路清晰的很嘿嘿

二分查找时mid是这个数就返回,现在要俩了,该咋弄?二分分分,我裂开来

算球了搜答案吧,哈哈哈

一百度,c++的,噢,原来有直接找大于、小于边界的函数呀,不行,我初学,要自己底层慢慢来,严肃脸。

搜下python的,噢,这呀,easy

def写写写,发呆呆,写个锤锤,再看下人家的吧,怎么忘这么快!

得了,复制下来背吧 2333333333

学渣思路

  1. 在nums里<=target的中,找索引最大的
  2. 在nums里>=target的中,找索引最小的
  3. 加个 [ ] 就它了
# python3
class Solution(object):
    def searchRange(self, nums, target):
        """
        :type nums: List[int]
        :type target: int
        :rtype: List[int]
        """
        left = self.lowwer_bound(nums, target)  # 调用时别忘 self.
        right = self.higher_bound(nums, target)
        if left == right:
            return [-1, -1]
        return [left, right - 1]  # 记得 right - 1

    def lowwer_bound(self, nums, target):
        # find 小边界 in range [left, right)
        left, right = 0, len(nums)  # 用的是 len(nums)
        while left < right:
            mid = int((left + right) / 2)  # 向下取整,所以后面left = mid + 1
            if nums[mid] < target:
                left = mid + 1  # 这里注意
            elif nums[mid] > target:
                right = mid
            else:  # nums[mid] == target: 两个函数在这里有所区别
                # 既然找到一个了,就快往左边再找找,看还有没有更小的索引对应的是target
                right = mid  
		return left  # right也行

    def higher_bound(self, nums, target):
        # find 大边界 in range [left, right)
        left, right = 0, len(nums)
        while left < right:
            mid = int((left + right) / 2)
            if nums[mid] < target:
                left = mid + 1
            elif nums[mid] > target:
                right = mid
            else:  # nums[mid] == target:
                left = mid + 1  # 往右边找有没有更大的index也是target
        return left

无数小坑

  • 考虑边界问题,比nums里所有的都小、或都大的值
  • 调用类的method忘记了加 self.
  • index是从0开始到len(nums)-1
  • 向下取整,所以后面left = mid + 1
  • nums[mid]等于target时,一个再往左找,一个再往右找
  • 最后return时,right要-1

含泪总结

  • 这些个索引、+1、-1的真滴头大,逻辑捋不清
  • 光看是学不会的,要多动手,多做多想多总结
  • 重要的是思路,有了大致方法后再着手写代码
  • 画图模拟来理清思路,边写边说以集中注意力
  • 活学活用,融会贯通,加油吧,渣渣文

代码参(fu)考(zhi)学(zhan)习(tie)的这位大神——负雪明烛的博客
https://blog.csdn.net/fuxuemingzhu/article/details/83273084

【深度学习-番外1】Win10系统搭建VSCode+Anaconda+Pytorch+CUDA深度学习环境和框架全过程 本文将介绍在Windows 10系统下搭建深度学习环境的完整过程,包括安装Anaconda、CUDA、NVIDIA显卡驱动以及Pytorch框架。后续本专栏的Python语言下的深度学习环境都以本篇搭建的为准。 阅读详情

相关推荐

如何在RT-DETR-R18中修改ultralytics的yaml文件(附完整代码)

本文详细指导如何在RT-DETR模型中,通过修改Ultralytics框架的yaml配置文件,将Backbone从ResNet50轻量化替换为ResNet18。核心步骤包括自定义ResNet18基础模块、调整HybridEncoder的expansion参数至0.5,并协同修改检测头Decoder层数,最终提供完整的配置文件代码,助力开发者在资源受限环境下部署轻量级实时目标检测模型。

weixin_29091445的博客 249

leetcode-Python】-二分搜索-34 Find First and Last Position of Element in Sorted Array

目录 题目链接 题目描述 示例 解决思路一 解决思路一Python实现 解决思路二 解决思路二Python实现 解决思路三 解决思路三Python实现 题目链接 34. Find First and Last Position of Element in Sorted Array 题目描述 给定非降序整数数组nums,找出元素值等于target的索引区间。如果数组中不存在target,返回[-1,-1]。 约束条件: 0 <= nums.length <= ..

gulaixiangjuejue的博客 304

OpenClaw实战系列02:企业微信接入完全指南

本文详细介绍了如何将OpenClaw与企业微信对接,实现智能机器人功能。主要内容包括:企业微信侧创建智能机器人或自建应用,获取必要凭证和权限;OpenClaw侧安装配置企业微信插件,设置通信通道;完成双向验证并进行功能测试。文章提供了详细的配置步骤、常见问题排查方法和进阶玩法,帮助用户在30分钟内完成对接。关键点包括:确保URL可访问、Token一致、网关正常运行,以及针对不同部署环境(云服务器/内网)的配置差异。

PeterPan的博客 3721

Leetcode 34. Find First and Last Position of Element in Sorted Array (python+cpp)

Leetcode 34. Find First and Last Position of Element in Sorted Array题目解法:二分查找 题目 解法:二分查找 思路非常简单: 二分法定位到一个和target相等的位置 因为是排好序的,所以从这个位置开始类似双指针的向外寻找lower bound和upper bound python代码如下: class Solution: ...

qq_37821701的博客 396

Find First and Last Position of Element in Sorted Array(python)

Given an array of integersnumssorted in ascending order, find the starting and ending position of a giventargetvalue. Your algorithm's runtime complexity must be in the order ofO(logn). If the...

MaggieTian77的博客 493

LeetCode34. Find First and Last Position of Element in Sorted Array 解题报告(Python & C++)

作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录题目描述题目大意解题方法二分查找参考资料日期 题目地址:https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array/description/ 题目描述 Given an ...

负雪明烛 1万+

LeetCode34. Find First and Last Position of Element in Sorted Array 解题报告(Python

题目分析: 这一题和【LeetCode】33. Search in Rotated Sorted Array极为相似,而且题目要求我们的时间复杂度也是O(log n),根据时间复杂度的提醒我们可以想到二分法。 代码说明: 代码先使用经典二分法找到一个符合的元素,然后再建立指针左右寻找符合的边界,代码中已有注释,请参考注释。 测试代码: class Solution: def searchR...

L141210113的专栏 560

LeetCode34. Find First and Last Position of Element in Sorted Array (思路及python解法)

Given an array of integersnumssorted in ascending order, find the starting and ending position of a giventargetvalue. Your algorithm's runtime complexity must be in the order ofO(logn). If the...

AIpush的博客 299

python3】leetcode 34. Find First and Last Position of Element in Sorted Array(Medium)

34. Find First and Last Position of Element in Sorted Array(Medium) Given an array of integers nums sorted in ascending order, find the starting and ending position of a given target value. Your al...

momottyy的专栏 390

LeetCode34. Find First and Last Position of Element in Sorted Array - Python

问题描述: 34. 在排序数组中查找元素的第一个和最后一个位置 给定一个按照升序排列的整数数组nums,和一个目标值target。找出给定目标值在数组中的开始位置和结束位置。 你的算法...

Growth Diary 1347

LeetCode34. Find First and Last Position of Element in Sorted Array(C++/Python

Given an array of integersnumssorted in ascending order, find the starting and ending position of a giventargetvalue. Your algorithm's runtime complexity must be in the order ofO(logn). If the...

qq_41562704的博客 276

python leetcode 34. Find First and Last Position of Element in Sorted Array

二分定位,再前后遍历 class Solution: def searchRange(self, nums, target): """ :type nums: List[int] :type target: int :rtype: List[int] """ if nums==[]: ...

Neekity的博客 498

leetcode(python) 34. Find First and Last Position of Element in Sorted Array找出排序数组中元素的第一个和最后一个位置

目录DescriptionExample题意解题思路code Description Find First and Last Position of Element in Sorted Array Given an array of integers nums sorted in ascending order, find the starting and ending position of...

347

34. Find First and Last Position of Element in Sorted Array [LeetCode]

34.Find First and Last Position of Element in Sorted Array Given an array of integersnumssorted in ascending order, find the starting and ending position of a giventargetvalue. Your algorith...

他的博客 1765

python/M/34Find First and Last Position of Element in Sorted Array

题目 这道题好像改过名字,原来应该叫Search for a Range(搜索范围)吧 基本思路 看到O(logn)的时间复杂度的查找,就首先想到二分查找,刚好这道题中数字是升序的,所以可以直接拿来用,但是我们要进行一点点小修改。当我们使用传统二分查找思路找到和target相等的值的索引的时候,我们继续分头向前向后循环,直到找到不等于target的值,此时就能找到我们需要的索引对。 代...

alicelmx的博客 1662

Leetcode34. Find First and Last Position of Element in Sorted Array

描述: Given an array of integers nums sorted in ascending order, find the starting and ending position of a given target value. Your algorithm’s runtime complexity must be in the order of O(log n). I...

syyyy712的博客 570

LeetCode-Find First and Last Position of Element in Sorted Array

LeetCode Java C++ Find First and Last Position of Element in Sorted Array

坟头草一米七五 647

Find First and Last Position of Element in Sorted Array

//分两部 先找最小 再找最大 class Solution {     public int[] searchRange(int[] nums, int target) {         if(nums == null || nums.length == 0) return new int[]{-1, -1};         int[] n = new int[] {-1, -1};    ...

yangzhuhappy的博客 485

[Leetcode][python]Find First and Last Position of Element in Sorted Array/在排序数组中查找元素的第一个和最后一个位置

题目大意 给定一个按照升序排列的整数数组 nums,和一个目标值 target。找出给定目标值在数组中的开始位置和结束位置。 你的算法时间复杂度必须是 O(log n) 级别。 如果数组中不存在目标值,返回 [-1, -1]。 解题思路 二分查找变种 代码 看到O(logn)的时间复杂度的查找,就首先想到二分查找,刚好这道题中数字是升序的,所以可以直接拿来用,但是我们要进行一点点小修...

蛮三刀酱 1512

34. Find First and Last Position of Element in Sorted Array

34. Find First and Last Position of Element in Sorted Array 题目链接如上 先给代码 class Solution { public: vector<int> searchRange(vector<int>& nums, int target) { int left = searchleft(nums,target); int right = searchright(nu

just_a_fresh_man的博客 324

LeetCode34 查找排序数组中指定元素的第一个和最后一个位置

Given an array of integers nums sorted in ascending order, find the starting and ending position of a given target value. Your algorithm’s runtime complexity must be in the order of O(log n). If the t...

fruit513的博客 254

LeetCode #34 - Find First and Last Position of Element in Sorted Array

题目描述: Given an array of integers nums sorted in ascending order, find the starting and ending position of a given target value. Your algorithm's runtime complexity must be in the order of O(log n). ...

LawFile的博客 691

XC7K325T 4片DDR内存读写测试(含教程和FPGA工程)

XC7K325T 4片DDR内存读写测试(含教程和FPGA工程),实现的4片64BIT,DDR3用MIG读写数据的测试,DDR3稳定运行于800M(1600M 数据时钟),附件有FPGA工程,操作说明,参考原理图

spwm_veccontrol_double_loops.slx

异步电机双闭环SPWM矢量控制,严格按照教材搭建,参数已经整定。

上一篇: Numpy中的各种stack
zhou_zw
博客等级 码龄8年 1粉丝 7原创
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值