Offer prove safety [42]. And the two numbers S (Python implementation)

Title Description

An incrementing input and a digital sorted array S, find the two numbers in the array, and so that they are exactly S, and if a plurality of digits equal to S, the product of the output of the minimum number of two.

Output Description:

Corresponding to each test case, the output of two numbers, the first small output.

Solution one: the double pointer Method

# -*- coding:utf-8 -*-
class Solution:
    def FindNumbersWithSum(self, array, tsum):
        if not array: return []
        left, right = 0, len(array) - 1
        while left < right:
            _sum = array[left] + array[right]
            if _sum > tsum:
                right -= 1
            elif _sum < tsum:
                left += 1
            else:
                return [array[left], array[right]]
        return []

 

Published 75 original articles · won praise 50 · views 10000 +

Guess you like

Origin blog.csdn.net/qq_36936730/article/details/104710669