【leetcode】第三大的数

 

int thirdMax(int* nums, int numsSize){
    long long first = -2147483649;
    long long second = -2147483649;
    long long third = -2147483649;
    for (int i=0; i<numsSize; i++)
    {
        if (nums[i] > first) 
        {
            long long temp = second;
            second = first;
            first = nums[i];
            third = temp;
        }
        else if (nums[i] > second && nums[i] < first) 
        {
            third = second;
            second = nums[i];
        }
        else if (nums[i] > third && nums[i] < second) third = nums[i];
    }
    if (third == -2147483649) return first;
    return third;
}

 

posted @ 2020-09-01 10:45  温暖了寂寞  阅读(201)  评论(0编辑  收藏  举报