-
-
Notifications
You must be signed in to change notification settings - Fork 298
/
Copy path704.py
25 lines (25 loc) · 943 Bytes
/
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
__________________________________________________________________________________________________
sample 260 ms submission
from bisect import bisect_left
class Solution:
def search(self, nums: List[int], target: int) -> int:
try:
ind = bisect_left(nums, target)
return ind if nums[ind]==target else -1
except:
return -1
__________________________________________________________________________________________________
sample 13992 kb submission
class Solution:
def search(self, nums: List[int], target: int) -> int:
l, r = 0, len(nums)-1
while l <= r:
mid = (l+r)//2
if nums[mid] < target:
l = mid+1
elif nums[mid] > target:
r = mid-1
else:
return mid
return -1
__________________________________________________________________________________________________