leetcode

来源:互联网 发布:淮南安广网络营业厅 编辑:程序博客网 时间:2024/06/11 04:15

原题:

414. Third Maximum Number

DescriptionHintsSubmissionsDiscussSolution
DiscussPick One

Given a non-empty array of integers, return the third maximum number in this array. If it does not exist, return the maximum number. The time complexity must be in O(n).

Example 1:

Input: [3, 2, 1]Output: 1Explanation: The third maximum is 1.

Example 2:

Input: [1, 2]Output: 2Explanation: The third maximum does not exist, so the maximum (2) is returned instead.

Example 3:

Input: [2, 2, 3, 1]Output: 1Explanation: Note that the third maximum here means the third maximum distinct number.Both numbers with value 2 are both considered as second maximum.
代码如下:
int thirdMax(int* nums, int numsSize) {    long int first=INT_MIN;    long int second=INT_MIN;    long int third=INT_MIN;    for(int n=0;n<numsSize;n++)    {        if(*(nums+n)>first)        {            third=second;            second=first;            first=*(nums+n);        }        else        {            if(*(nums+n)>second&&*(nums+n)!=first)            {                third=second;                second=*(nums+n);            }            else            {                if(*(nums+n)>third&&*(nums+n)!=second&&*(nums+n)!=first)                {                    third=*(nums+n);                }            }        }    }    if(third==INT_MIN)        return first;    return third;}

这个leetcode对c真的是特别的不友好。算法很简单,这个平台对数据的处理太不友好了。这个并不能提交,因为在极值上总是出问题。