第一次上九十 | #二分查找-II#
二分查找-II
http://www.nowcoder.com/practice/4f470d1d3b734f8aaf2afb014185b395
class Solution: def search(self , nums , target ): # write code here if not nums: return -1 left, right = 0, len(nums)-1 mark = False while left<=right: median = left + (right-left)//2 if nums[median]>target:right = median-1 elif nums[median]==target:right = median-1;mark = True else: left = median+1 return left if mark else -1
二分法的边界和返回值总是很难确定,有没有什么技巧呀