首页 > 代码库 > LeetCode "Third Maximum Number"
LeetCode "Third Maximum Number"
Straight-forward strategy.. please take care of all details - data type, assignment order etc.
class Solution {public: int thirdMax(vector<int>& nums) { long long v1, v2, v3; v1 = v2 = v3 = std::numeric_limits<long long>::min(); size_t n = nums.size(); size_t cnt = 0; for(int i = 0; i < n; i ++) { int v = nums[i]; if(v == v1 || v == v2 || v == v3) continue; cnt ++; if(v > v1) { v3 = v2; v2 = v1; v1 = v; } else if(v > v2) { v3 = v2; v2 = v; } else if(v > v3) { v3 = v; } } return cnt < 3 ? v1 : v3; }};
LeetCode "Third Maximum Number"
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。