728x90
탐색 알고리즘
- 순차탐색
- 해시구조
- 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 |
댓글