面试常见算法题---二分查找 python

注意判断找不到num的情况,另外移动的时候可以多移动一位,查找速度更快。

class Solution():
    def seek(self, s, target):
        i = 0
        j = len(s)-1
        while i < j:
            m = (i + j)//2
            if s[m] == target:
                return m
            elif s[m] > target:
                j = m-1
            else:
                i = m+1
        return None

你可能感兴趣的:(python学习)