-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary_search.py
More file actions
28 lines (24 loc) · 897 Bytes
/
Copy pathbinary_search.py
File metadata and controls
28 lines (24 loc) · 897 Bytes
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
# 704. Binary Search
# binary tree search
# 어디서 찾을지 정하기 array 의 시작 & 끝 계속 줄이면서 찾기
# Given an array of integers nums which is sorted in ascending order, and an integer target,
# write a function to search target in nums. If target exists, then return its index.
# Otherwise, return -1.
# You must write an algorithm with O(log n) runtime complexity.
class Solution(object):
def search(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: int
"""
start = 0
end = len(nums) - 1
while(start <= end):
if(nums[(start + end)//2] == target):
return (start + end) //2
elif(nums[(start + end)//2] > target):
end = (start + end)//2 - 1
else:
start = (start + end)//2 + 1
return -1