Uriel's Corner

Research Associate @ Harvard University / Research Interests: Computer Vision, Biomedical Image Analysis, Machine Learning
posts - 0, comments - 50, trackbacks - 0, articles - 594
实现二分查找,大水题


 1 #704
 2 #Runtime: 178 ms (Beats 94.20%)
 3 #Memory: 14.8 MB (Beats 31.81%)
 4 
 5 class Solution(object):
 6     def search(self, nums, target):
 7         """
 8         :type nums: List[int]
 9         :type target: int
10         :rtype: int
11         """
12         l = 0
13         r = len(nums)
14         while l < r:
15             mid = (l + r) // 2
16             if nums[mid] == target:
17                 return mid
18             if nums[mid] < target:
19                 l = mid + 1
20             else:
21                 r = mid
22         return -1

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   博问   Chat2DB   管理