알고리즘/알고리즘 문제풀이

이진트리_LeetCode_binary-search

Downey 2021. 9. 2. 18:44

https://leetcode.com/problems/binary-search/submissions/

 

Binary Search - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

 

접근 방법

  1. findIndex(where:), guard 문을 조합하여 index를 확인, else의 경우 -1을 리턴
  2. 확인된 인덱스를 return

근데 이건 이진탐색이 아닌데,,?

차후 sett의 블로그를 참조하여 재 진행 하자.

정답 코드

Runtime: 488 ms, faster than 11.32% of Swift online submissions for Binary Search.

Memory Usage: 14.1 MB, less than 84.15% of Swift online submissions for Binary Search.

더보기
class Solution {
    func search(_ nums: [Int], _ target: Int) -> Int {
        guard let output = nums.firstIndex(where: {$0 == target}) else {
            return -1
        }
        return output
    }
}