And s is two digits -python

Thinking: Because the array is in ascending order, and the same number of two, separated farther, the smaller the volume, and therefore with two pointers, one from the front to find a first one is trying to find a minimum of a group

# -*- coding:utf-8 -*-
class Solution:
    def FindNumbersWithSum(self, array, tsum):
        # write code here
        p1 = 0
        p2 = len(array)-1
        while p2>=p1:
            if array[p1] + array[p2] == tsum:
                return [array[p1], array[p2]]
            elif array[p1] + array[p2] > tsum:
                p2 -= 1
            else:
                p1 += 1
        return []

Guess you like

Origin www.cnblogs.com/dolisun/p/11332534.html