Python - Find second smallest number

前端 未结 16 1163
长发绾君心
长发绾君心 2020-11-28 10:35

I found this code on this site to find the second largest number:

def second_largest(numbers):
    m1, m2 = None, None
    for x in numbers:
        if x >         


        
16条回答
  •  时光取名叫无心
    2020-11-28 11:19

    Here is:

    def find_second_smallest(a: list) -> int:
        first, second = float('inf')
        for i in range(len(a)):
            if a[i] < first:
                first, second = a[i], first
            elif a[i] < second and a[i] != first:
                second = a[i]
        return second
    

    input: [1, 1, 1, 2]
    output: 2

提交回复
热议问题