-
이진트리_LeetCode_binary-search알고리즘/알고리즘 문제풀이 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
접근 방법
- findIndex(where:), guard 문을 조합하여 index를 확인, else의 경우 -1을 리턴
- 확인된 인덱스를 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 } }
'알고리즘 > 알고리즘 문제풀이' 카테고리의 다른 글
DP_LeetCode_house-robber (0) 2021.09.10 순열_LeetCode_permutation-in-string (0) 2021.09.08 배열_LeetCode_Rotate Array (0) 2021.08.30 큐_LeetCode_find-the-winner-of-the-circular-game (0) 2021.08.26 해쉬_Leetcode_rabbits-in-forest (0) 2021.08.25