Algorithm/LeetCode
이진탐색
HaningYa
2021. 1. 19. 15:45
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