본문 바로가기
Algorithm/LeetCode

이진탐색

by HaningYa 2021. 1. 19.
728x90
 

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

탐색 알고리즘

  • 순차탐색
  • 해시구조
  • BST
  • 이진탐색
class Solution {
    func search(_ nums: [Int], _ target: Int) -> Int {
        var head = 0
        var tail = nums.count-1
        while head <= tail {
            print(Array(nums[head...tail]))
            let pivot = head + (tail-head)/2
            if nums[pivot] == target {
                return pivot
            }
            if nums[pivot] < target {
                head = pivot
                head += 1
            }else {
                tail = pivot
                tail -= 1
            }
        }
        return -1
    }
}
728x90

'Algorithm > LeetCode' 카테고리의 다른 글

LRU cache (Swift)  (0) 2021.01.29
876. Middle of the Linked List  (0) 2021.01.28
Traversing Tree (recursive, iterative)  (0) 2021.01.18
LeetCode - Remove Duplicates from Sorted Array  (0) 2020.09.11
LeetCode - Fibonacci Number, Climbing Stairs  (0) 2020.09.10

댓글