[LeetCode]#2089. Find Target Indices After Sorting Array

Input: nums = [1,2,5,2,3], target = 2
Output: [1,2]
Explanation: After sorting, nums is [1,2,2,3,5].
The indices where nums[i] == 2 are 1 and 2.
  1. Sort it.
  2. find the target in sorted nums.
  3. return its index.
class Solution:
def targetIndices(self, nums, target):
nums.sort()
ans=[]
for i in range(len(nums)):
if nums[i]==target:
ans.append(i)
return ans

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Fatboy Slim

Fatboy Slim

Interesting in any computer science.