面试题3-找出一组数中第二大的数

 

问题:找出一组数中第二大的数

def find_second_large_num(num_list):
    """找出第二大的数字"""
    # tmp_list = sorted(num_list)
    # print("Second_large_num is:", tmp_list[-2]
    
    # 设置两个标志一个存储最大数one, 一个存储次大数two
    # 遍历一次即可,先选出one, 再选出two
    one = num_list[0]
    two = num_list[0]
    for i in range(1, len(num_list)):
        if num_list[i] > one:
            two = one
            one = num_list[i]
        elif num_list[i] > two:
             two = num_list[i]
        else:
            pass
    print("Second_large_num is: ",  two)


if __name__ == '__main__':
    test_list=[34,11,23,56,78,0,9,12,3,7,5]
    find_Second_large_num(test_list)
    

 

上一篇:How does SqlDataReader handle really large queries?(转载)


下一篇:Mysql:Optimizing Memory Use、Enabling Large Page Support:内存使用优化:【global + session】