414.第三大的数
程序员文章站
2022-04-25 14:42:26
...
class Solution {
public:
int thirdMax(vector<int>& nums) {
int num1, num2, num3;
num1 = INT_MIN;
num2 = INT_MIN;
num3 = INT_MIN;
for (int i = 0; i < nums.size(); i++)
num1 = max(num1,nums[i]);
for (int i = 0; i < nums.size(); i++)
if(nums[i]!=num1)
num2 = max(num2, nums[i]);
bool flag = false;
for (int i = 0; i < nums.size(); i++)
if (nums[i] != num1 && nums[i] != num2)
{
num3 = max(num3, nums[i]);
flag = true;
}
if (flag)
return num3;
else
return num1;
}
};