[LeetCode 414] Third Maximum Number
来源:互联网 发布:手机版电子杂志软件 编辑:程序博客网 时间:2024/05/22 00:20
Question
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: 1
Explanation: The third maximum is 1.
Example 2:
Input: [1, 2]
Output: 2
Explanation: The third maximum does not exist, so the maximum (2) is returned instead.
Example 3:
Input: [2, 2, 3, 1]
Output: 1
Explanation:
Note that the third maximum here means the third maximum distinct number.Both numbers with value 2 are both considered as second maximum.思路
题目对于时间要求 O(n), 只能遍历一次数组, 在遍历中只要维护 first_max, second_max, third_max就好了。用 flash来标识三个最大值的赋值情况:
flash=0 应该给 fist_max 赋值,flash=1 应该给 second_max 赋值flash=2 应该给 third_max赋值flash=3 更新三个值
- 代码:
class Solution {public: int thirdMax(vector<int>& nums) { int first_max=0, second_max=0, third_max=0; int flash=0; for(auto &a:nums){ switch(flash){ case 0: first_max=a; ++flash; break; case 1: if(a > first_max){ second_max=first_max; first_max=a; ++flash; } else if(a<first_max){ second_max=a; ++flash; } break; case 2: if(a> first_max){ third_max=second_max; second_max=first_max; first_max=a; ++flash; } else if(first_max > a && a > second_max){ third_max=second_max; second_max=a; ++flash; } else if(a<second_max){ third_max=a; ++flash; } break; default: if(a>first_max){ third_max=second_max; second_max=first_max; first_max=a; } else if(first_max>a && a>second_max){ third_max=second_max; second_max=a; } else if(second_max>a && a>third_max) third_max=a; break; } } return flash>2? third_max:first_max; }};
这个代码的可读性很差,我在网上参考其他人的代码后优化如下:
class Solution {public: int thirdMax(vector<int>& nums) { int first_max = nums[0], second_max = INT_MIN, third_max = INT_MIN; int flash = 0; for (auto &a : nums) { if ((flash>2 && a<third_max) || (flash>=0 && a == first_max) || (flash>=1 && a == second_max) || (flash>=2 && a == third_max)) continue; ++flash; if (a>first_max) { third_max = second_max; second_max = first_max; first_max = a; } else if (a>second_max){ third_max = second_max; second_max = a; } else if(a>third_max){ third_max = a; } } return flash>1 ? third_max : first_max;}};
- LeetCode #414: Third Maximum Number
- Leetcode Third Maximum Number 414
- [LeetCode]414 Third Maximum Number
- LeetCode 414 --- Third Maximum Number
- Leetcode 414:Third Maximum Number
- [LeetCode 414] Third Maximum Number
- Leetcode 414 Third Maximum Number
- LeetCode 414 Third Maximum Number
- LeetCode.414 Third Maximum Number
- LeetCode Third Maximum Number
- LeetCode-Third Maximum Number
- [Leetcode] Third Maximum Number
- LeetCode: Third Maximum Number
- leetcode---Third Maximum Number
- LeetCode : Third Maximum Number
- leetcode[Third Maximum Number]
- LeetCode Third Maximum Number
- leetCode-Third Maximum Number
- 2017暑假七林集训day4
- React开发技巧总结
- [sicily]1001 会议安排
- Linux常用命令大全
- HASH 密码学
- [LeetCode 414] Third Maximum Number
- dbutils 介绍
- kali下automater的基本使用
- visual studio 2015 调试
- servlet cookie 处理
- Windowleaked错误
- 卡尔曼滤波的opencv源码及编程步骤分析
- E Chart学习 one
- 因配置不对,导致出现The requested resource is not available的解决方法