当有简单方法可以使用时,千万别饶了远路。
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