1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| inline bool check(vector<int> &nums, int i, int index) { return index < 0 || nums[i] > nums[index]; } int thirdMax(vector<int>& nums) { int first = -1, second = -1, thrid = -1; for(int i = 0, size = nums.size();i < size;i++) { if (first >= 0 && second >= 0 && check(nums, i, thrid)) { if (nums[i] > nums[first]) { thrid = second; second = first; first = i; } else if (nums[i] != nums[first] && nums[i] > nums[second]) { thrid = second; second = i; } else if (nums[i] != nums[first] && nums[i] != nums[second]) { thrid = i; } } else if (first >= 0 && check(nums, i, second)) { if (nums[i] > nums[first]) { second = first; first = i; } else if (nums[i] != nums[first]) second = i; } else if (check(nums, i, first)) { first = i; } } if (thrid == -1) return nums[first]; else return nums[thrid]; }
|