-
Notifications
You must be signed in to change notification settings - Fork 889
/
SearchForARange.swift
48 lines (38 loc) · 1.37 KB
/
SearchForARange.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
/**
* Question Link: https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array/
* Primary idea: Binary Search, check left or right separately
*
* Time Complexity: O(logn), Space Complexity: O(1)
*/
class SearchForARange {
func searchRange(_ nums: [Int], _ target: Int) -> [Int] {
guard !nums.isEmpty else {
return [-1, -1]
}
return [searchStartIdx(nums, target), searchEndIdx(nums, target)]
}
private func searchStartIdx(_ nums: [Int], _ target: Int) -> Int {
var left = 0, right = nums.count - 1
while left + 1 < right {
let mid = (right - left) / 2 + left
if nums[mid] < target {
left = mid + 1
} else {
right = mid
}
}
return nums[left] == target ? left : nums[right] == target ? right : -1
}
private func searchEndIdx(_ nums: [Int], _ target: Int) -> Int {
var left = 0, right = nums.count - 1
while left + 1 < right {
let mid = (right - left) / 2 + left
if nums[mid] > target {
right = mid - 1
} else {
left = mid
}
}
return nums[right] == target ? right : nums[left] == target ? left : -1
}
}