본문 바로가기
코딩 테스트/leetcode

Find Target Indices After Sorting Array

by ornni 2024. 7. 29.
728x90
반응형

첫번째 코드

 

먼저 순서대로 정렬을 한 후에

target값과 동일한 경우 해당 인덱스를 가져오는 코드를 작성한다.

 

class Solution:
    def targetIndices(self, nums: List[int], target: int) -> List[int]:
        answer = []
        nums.sort()
        index = 0
        
        for i in nums:
            if i == target:
                answer.append(index)
            index += 1
        
        return answer

 

통과


링크

https://github.com/ornni/leetcode/tree/main/2089-find-target-indices-after-sorting-array

 

leetcode/2089-find-target-indices-after-sorting-array at main · ornni/leetcode

Collection of LeetCode questions to ace the coding interview! - Created using [LeetHub v3](https://github.com/raphaelheinz/LeetHub-3.0) - ornni/leetcode

github.com

 

반응형

'코딩 테스트 > leetcode' 카테고리의 다른 글

Baseball Game  (0) 2024.08.05
Deepest Leaves Sum  (0) 2024.08.02
Top K Frequent Elements  (0) 2024.07.19
Delete Greatest Value in Each Row  (0) 2024.07.15
Number of Good Pairs  (0) 2024.07.12