offer412--序列和为s

# coding=utf-8
def searchs(array, k):
    if array is None:
        return 0
    begin, end = 0, 0
    s = array[0]
    while begin < len(array) and end < len(array) and begin <= end:
        #s = 0
        #for i in range(begin, end + 1):
        #    s += array[i]
        #    pass
        if s > k:
            s -= array[begin]
            begin += 1
        elif s < k:
            end += 1
            if end == len(array):
                break
            s += array[end]
        else:
            print [array[item] for item in range(begin, end + 1)]
            s -= array[begin]
            begin += 1
            end += 1
            if end == len(array):
                break
            s += array[end]


if __name__ == '__main__':
    array = [1, 2, 3, 4, 5, 6, 7, 8, 9]
    searchs(array, 15)

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