Search Insert Position @ LeetCode (Python)
kitt
posted @ 2014年2月14日 19:42
in LeetCode
, 1869 阅读
class Solution: # @param A, a list of integers # @param target, an integer to be inserted # @return integer def searchInsert(self, A, target): left = 0; right = len(A) - 1 while left <= right: mid = (left + right) / 2 if A[mid] == target: return mid elif A[mid] < target: left = mid + 1 else: right = mid - 1 return mid if target < A[mid] else mid + 1