2089. Find Target Indices After Sorting Array

约 1 分钟

LeetCode 每日一题:Find Target Indices After Sorting Array

LeetCode 原题

You are given a 0-indexed integer array nums and a target element target.

A target index is an index i such that nums[i] == target.

Return a list of the target indices of nums after sorting nums in non-decreasing order. If there are no target indices, return an empty list. The returned list must be sorted in increasing order.

思路

其实直接写很简单 当然完全存在优化的空间。比如,提前遍历一遍数组,找到比target小的个数与target的个数,然后直接生成就行了,这样复杂度能低一些

python
class Solution:
    def targetIndices(self, nums: list[int], target: int) -> list[int]:
        nums.sort()
        res = []
        for i, num in enumerate(nums):
            if num == target:
                res.append(i)
        return res

返回文章列表