-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
704 Binary Search.swift
44 lines (36 loc) 路 1.09 KB
/
704 Binary Search.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
//
// 704 Binary Search.swift
// LeetCode-Solutions
//
// Created by Aleksandar Dinic on 08/10/2020.
// Copyright 漏 2020 Aleksandar Dinic. All rights reserved.
//
import Foundation
/// Source: https://leetcode.com/problems/binary-search/
class Solution {
/// Finds the index of `target` in the given array.
///
/// - Parameters:
/// - nums: A sorted (in ascending order) integer array.
/// - target: The target value.
/// - Returns: If the target exists in an array returns its index, otherwise
/// returns -1.
///
/// - Complexity:
/// - time: O(log(n)), where n is the length of the `nums`.
/// - space: O(1), only constant space is used.
func search(_ nums: [Int], _ target: Int) -> Int {
var low = 0
var high = nums.count - 1
while low <= high {
var mid = low + (high - low) / 2
guard nums[mid] != target else { return mid }
if nums[mid] < target {
low = mid + 1
} else {
high = mid - 1
}
}
return -1
}
}