【LeetCode】2089. 找出数组排序后的目标下标

当有简单方法可以使用时,千万别饶了远路。

1. 题目

2. 分析

3. 代码

python 复制代码
class Solution:
    def targetIndices(self, nums: List[int], target: int) -> List[int]:
        nums.sort() # 按照从小到大排序
        
        left, right = 0, len(nums)-1
        while(left <= right):
            mid = (left+right)//2
            if nums[mid] >= target:
                right = mid-1
            elif nums[mid] < target:
                left = mid+1
        
        res = []
        for i in range(left, len(nums)):
            if nums[i] == target:
                res.append(i)
            else:
                break
        return res
相关推荐
wenyq71 小时前
LeetCode 2460. Apply Operations to an Array
算法·leetcode
.格子衫.2 小时前
032动态规划之区间DP——算法备赛
算法·动态规划
青 春 记 忆2 小时前
LeetCode 142. 环形链表 II|Python 解法详解
python·leetcode·链表
不会代码的小猴3 小时前
7. JSON
开发语言·c++·笔记·qt·算法·json
怪奇云呼军3 小时前
知识库也会注入指令?闪电智能VoiceAgent 如何防住 Prompt Injection
人工智能·python·算法·云计算·音视频
阿里云大数据AI技术4 小时前
基于 EMR Serverless Ray 实现 Qwen 模型批量推理实践
人工智能·算法·agent
Rambo.xia5 小时前
为什么去马赛克算法,决定了ISP的画质上限
算法·接口隔离原则
Benny_Tang5 小时前
题解:P10230 [COCI 2023/2024 #4] Lepeze
c++·算法
Geek-Chow5 小时前
06 训练管线:数据如何变成权重
人工智能·算法