Skip to content

Commit e101612

Browse files
authored
Merge pull request #539 from diss1993/ticket/704
Swift | 704. Binary Search
2 parents 601dc14 + 21e932a commit e101612

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

swift/704-Binary-Search

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution {
2+
func search(_ nums: [Int], _ target: Int) -> Int {
3+
var l = 0
4+
var r = nums.count - 1
5+
while l <= r {
6+
let mid = (l + r) / 2
7+
guard nums[mid] != target else { return mid }
8+
l = nums[mid] < target ? mid + 1 : l
9+
r = nums[mid] > target ? mid - 1 : r
10+
}
11+
return -1
12+
}
13+
}

0 commit comments

Comments
 (0)