-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path704.py
52 lines (39 loc) · 1.59 KB
/
704.py
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
# 704二分查找
# 左闭右开区间写法
class Solution:
def search(self, nums: List[int], target: int) -> int:
# 首先判断是不是越界的target
if target not in nums:
return -1
# 定义一个两个index 第一个是开头 第二个是结尾
flag_left, flag_right = 0, len(nums)
while flag_left < flag_right:
# 定义中间值=左边指针偏移量+左右指针中间值
value_index = flag_left + (flag_right - flag_left) // 2
# 找到对应值
if target == nums[value_index]:
return value_index
if target < nums[value_index]:
flag_right = value_index
if target > nums[value_index]:
flag_left = value_index + 1
return -1
# 左闭右闭区间写法
class Solution:
def search(self, nums: List[int], target: int) -> int:
# 首先判断是不是越界的target
if target not in nums:
return -1
# 定义一个两个index 第一个是开头 第二个是结尾
flag_left, flag_right = 0, len(nums)-1
while flag_left <= flag_right:
# 定义中间值=左边指针偏移量+左右指针中间值
value_index = flag_left + (flag_right - flag_left) // 2
# 找到对应值
if target == nums[value_index]:
return value_index
if target < nums[value_index]:
flag_right = value_index - 1
if target > nums[value_index]:
flag_left = value_index + 1
return -1