문제를 이해하고 있다면 바로 풀이를 보면 됨
전체 코드로 바로 넘어가도 됨
마음대로 번역해서 오역이 있을 수 있음
인덱스가 0부터 시작하는 정수 배열 nums와 타겟 요소인 target이 주어진다.
타겟 인덱스는 nums[i] == target을 만족하는 인덱스 i이다.
nums를 비감소 순으로 정렬한 후 타겟 인덱스의 리스트를 반환해라. 대상 인덱스가 없다면 빈 리스트를 반환해라. 반환되는 리스트는 오름차순으로 정렬되어야 한다.
#1
Input: nums = [1, 2, 5, 2, 3], target = 2
Output: [1, 2]
Explanation: 정렬 후 nums는 [1, 2, 2, 3, 5]이다.
nums[i] == 2인 인덱스는 1과 2이다.
#2
Input: nums = [1, 2, 5, 2, 3], target = 3
Output: [3]
Explanation: 정렬 후 nums는 [1, 2, 2, 3, 5]이다.
nums[i] == 3인 인덱스는 3이다.
#3
Input: nums = [1, 2, 5, 2, 3], target = 5
Output: [4]
Explanation: 정렬 후 nums는 [1, 2, 2, 3, 5]이다.
nums[i] == 5인 인덱스는 4이다.
class Solution {
public List<Integer> targetIndices(int[] nums, int target) {
int lessNum = 0;
int equalNum = 0;
for(int num : nums){
if(num < target){
lessNum++;
}else if(num == target){
equalNum++;
}
}
List<Integer> result = new ArrayList<>();
for(int i = lessNum; i < lessNum + equalNum; i++){
result.add(i);
}
return result;
}
}