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 ofnums after sortingnumsinnon-decreasing order. If there are no target indices, return anempty list. The returned list must be sorted in increasing order.
classSolution {
public List<Integer>targetIndices(int[] nums, int target) {
Arrays.sort(nums);
List<Integer> ans =new ArrayList<>();
for (int i = 0; i < nums.length; ++i) {
if (nums[i]== target) ans.add(i);
}
return ans;
}
}
1
2
3
4
5
6
7
8
9
10
classSolution {
funtargetIndices(nums: IntArray, target: Int): List<Int> {
nums.sort()
val ans = mutableListOf<Int>()
for (i in nums.indices) {
if (nums[i] == target) ans.add(i)
}
return ans
}
}
1
2
3
4
5
6
7
8
classSolution:
deftargetIndices(self, nums: list[int], target: int) -> list[int]:
nums.sort()
ans = []
for i, v in enumerate(nums):
if v == target:
ans.append(i)
return ans
1
2
3
4
5
6
7
8
9
10
11
12
13
impl Solution {
pubfntarget_indices(nums: Vec<i32>, target: i32) -> Vec<i32> {
letmut nums = nums;
nums.sort();
letmut ans =vec![];
for (i, &v) in nums.iter().enumerate() {
if v == target {
ans.push(i asi32);
}
}
ans
}
}
1
2
3
4
5
6
7
8
9
10
classSolution {
targetIndices(nums: number[], target: number):number[] {
nums.sort((a, b) =>a-b);
constans: number[] = [];
for (leti=0; i<nums.length; ++i) {
if (nums[i] ===target) ans.push(i);
}
returnans;
}
}