我一直觉得二分类写法变来变去,有时候+1, 有时候-1,完全摸不着头脑。直到我上了某章的算法课,里面讲到了二分法模版,变形,我才恍然大悟。
二分法模版
题目见https://www.lintcode.com/problem/first-position-of-target/description
我用的模版
class Solution:
"""
@param nums: The integer array.
@param target: Target to find.
@return: The first position of target. Position starts from 0.
"""
def binarySearch