【Leetcode】python - [2089] Find Target Indices After Sorting Array 個人解法筆記

整理 LeetCode #2089 的個人解法筆記:解題思路、Time/Space Complexity 與邊界條件。

題目出處

2089. Find Target Indices After Sorting Array

難度

easy

題目分類

Array, Binary Search, Sorting

2026-10-20 一刷

個人範例程式碼 - 一刷 (2026/10/20)

class Solution:
    def targetIndices(self, nums: list[int], target: int) -> list[int]:
        count_lower = 0
        count_target = 0
        for num in nums:
            if num < target:
                count_lower += 1
            elif num == target:
                count_target += 1

        ans = []
        for i in range(count_target):
            ans.append(count_lower + i)

        return ans

算法說明

簡單的數學題目,

  1. 計算低於 target 的,可以知道從第幾個位置開始。
  2. 計算 target 有幾個,可以知道有重複幾個 target。

Time Complexity

O(n)

Space Complexity

O(1)(不含 output)

Licensed under CC BY-NC-SA 4.0
最後更新 Oct 20, 2026
使用 Hugo 建立
主題 Stack 由 Jimmy 設計