[LeetCode] 2089. Find Target Indices After Sorting Array

Chobby·5일 전
1

LeetCode

목록 보기
665/710

😎풀이

  1. nums 오름차 순 정렬
  2. 정렬된 배열 순회
    2-1. i번째 요소가 target과 같다면, 정답 배열에 추가
  3. target과 동일한 요소들의 인덱스 반환
function targetIndices(nums: number[], target: number): number[] {
    const sorted = nums.toSorted((a, b) => a - b)
    const indices = []
    for(let i = 0; i < sorted.length; i++) {
        if(sorted[i] === target) indices.push(i)
    }
    return indices
};
profile
내 지식을 공유할 수 있는 대담함

0개의 댓글