(leetcode题解)Third Maximum Number 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...
Leetcode 数组:414 第三大的数 third-maximum-number 给定一个非空数组,返回此数组中第三大的数。如果不存在,则返回数组中最大的数。要求算法时间复杂度必须是O(n)。 classSolution:defthirdMax(self, nums: List[int]) ->int: nums=list(set(nums)) MAX= float('-inf') SECEND_MAX= float('-inf')...
}inta = nums[0];intb =0;intc =0;boolsecond =false;// the second biggest number has foundboolthird =false;// the third biggest number has foundfor(inti =1; i < sz; i++){// cout<<a<<", "<<b<<", "<<c<<endl;if(a == nums[i]){continue; }elseif(a < nums[i]){if(...
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] Out...
LeetCode : Third Maximum Number 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]...
Explanation: Note that the third maximum here means the third maximum distinct number. Both numbers with value 2 are both considered as second maximum. Solution 1 用两个标识符表示,第二大的数和第三大的数是否已经赋值。 classSolution{public:intthirdMax(vector<int>& nums){if(nums.size() ==1...
【leetcode】414. Third Maximum Number problem 414. Third Maximum Number solution 思路:用三个变量first, second, third来分别保存第一大、第二大和第三大的数,然后遍历数组。 classSolution {public:intthirdMax(vector<int>&nums) {//1.LONG_MIN;longfirst = LONG_MIN, second = LONG_MIN, third = ...
Can you solve this real interview question? Third Maximum Number - Given an integer array nums, return the third distinct maximum number in this array. If the third maximum does not exist, return the maximum number. Example 1: Input: nums = [3,2,1]
leetcode-414-Third Maximum Number 题目描述: 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]...
https://github.com/grandyang/leetcode/issues/414 类似题目: Kth Largest Element in an Array Neither Minimum nor Maximum 参考资料: https://leetcode.com/problems/third-maximum-number https://leetcode.com/problems/third-maximum-number/solutions/90209/short-easy-c-using-set/ ...