題目出處
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
算法說明
簡單的數學題目,
- 計算低於 target 的,可以知道從第幾個位置開始。
- 計算 target 有幾個,可以知道有重複幾個 target。
Time Complexity
O(n)
Space Complexity
O(1)(不含 output)