Wins the offer - and two number s

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.

 

Thinking

Double pointer. The first title to prove safety problems and offer the like. The first problem is to find the target in a sort of two-dimensional matrix, also with a two-pointer

class Solution:
    def FindNumbersWithSum(self, array, tsum):
        if not array:
            return []
        
        i = 0
        j = len(array)-1
        while i < j:
            if array[i]+array[j]<tsum:
                i+=1
            elif array[i]+array[j]>tsum:
                j-=1
            else:
                break
        if array[i]+array[j]==tsum:
            return array[i],array[j]
        else:
            return []

 

Published 82 original articles · won praise 2 · Views 4351

Guess you like

Origin blog.csdn.net/qq_22498427/article/details/104818746