-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
33 Search in Rotated Sorted Array.swift
55 lines (46 loc) · 1.45 KB
/
33 Search in Rotated Sorted Array.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
45
46
47
48
49
50
51
52
53
54
55
//
// 33 Search in Rotated Sorted Array.swift
// LeetCode-Solutions
//
// Created by Aleksandar Dinic on 23/02/2021.
// Copyright © 2021 Aleksandar Dinic. All rights reserved.
//
import Foundation
/// Source: https://leetcode.com/problems/search-in-rotated-sorted-array/
class Solution {
/// Finds the index of `target` in the array.
///
/// - Parameters:
/// - nums: The array.
/// - target: An integer.
/// - Returns: The index of `target` if `target` exist in `nums`, otherwise
/// returns -1.
///
/// - Complexity:
/// - time: O(log n), where n is the length of `nums`.
/// - space: O(1), only constant space is used.
func search(_ nums: [Int], _ target: Int) -> Int {
var start = 0
var end = nums.count - 1
while start <= end {
let mid = start + (end - start) / 2
guard nums[mid] != target else { return mid }
if nums[start] < nums[mid] {
if nums[start] <= target, target < nums[mid] {
end = mid - 1
} else {
start = mid + 1
}
} else if nums[start] > nums[mid] {
if nums[mid] < target, target <= nums[end] {
start = mid + 1
} else {
end = mid - 1
}
} else {
start = mid + 1
}
}
return -1
}
}