704. 二分查找

题目链接:https://leetcode-cn.com/problems/binary-search/

执行用时:280 ms, 在所有 Swift 提交中击败了92.26% 的用户
内存消耗:13.9 MB, 在所有 Swift 提交中击败了26.13% 的用户

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Solution {
func search(_ nums: [Int], _ target: Int) -> Int {
var left = 0, right = nums.count - 1
var index = -1
while left <= right {
index = left + (right-left)/2
if nums[index] == target {
return index
}
if nums[index] < target {
left = index + 1
}else {
right = index - 1
}
}
return -1
}
}


704. 二分查找
https://pisces34.github.io/2021/08/18/leetcode/704/
发布于
2021年8月18日
许可协议