if len(nums)>=3: return nums[2] else:return nums[0] sol=Solution() print sol.thirdMax([1, 2])
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')...
Given a non-empty array of integers,returnthe third maximum number inthisarray. If it does not exist,returnthe maximum number. The time complexity must be in O(n). Example1: Input: [3, 2, 1] Output:1Explanation: The third maximum is1. Example2: Input: [1, 2] Output:2Explanation:...
Explanation: Note that the third maximum here means the third maximumdistinctnumber. Both numberswithvalue2are both consideredassecond maximum. 思路: 先通过归并排序把数组有序化,然后除去数组中重复的元素,最后拿到第三大的元素。 Python中有个collections模块,它提供了个类Counter,用来跟踪值出现了多少次。注意...
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] Ou......
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]
leetcode:Third Maximum Number Third Maximum Number 在非空整数数组中找到倒数第三大的数;如果长度小于三,返回最大的数;第三个最大不同数字。 总结: python中表示正负无穷: 代码中不考虑重复的时候可用set()函数创建一个无序不重复元素集。...The third Blog 写博客的第三天 接下来是我们要打开一个文件,...