Third Maximum Number

    这道题为简单题

  题目:

    

  思路:

    我直接设置三个变量分别储存前三大值,遍历整个列表,然后就是与三个值分别比较,并且每次num += 1,如果最后num大于3,那么说明前三大值存在,返回f3,否则不存在返回f1

  代码: 

 1 class Solution(object):
 2     def thirdMax(self, nums):
 3         """
 4         :type nums: List[int]
 5         :rtype: int
 6         """
 7         f1 = float(‘-Inf‘)
 8         f2 = float(‘-Inf‘)
 9         f3 = float(‘-Inf‘)
10         num = 0
11         for i in nums:
12             if i > f1:
13                 f3 = f2
14                 f2 = f1
15                 f1 = i
16                 num += 1
17             elif f1 > i and i > f2:
18                 f3 = f2
19                 f2 = i
20                 num += 1
21             elif f2 > i and i > f3:
22                 f3 = i
23                 num += 1
24         if num < 3: return f1
25         else: return f3
时间: 2024-10-31 09:57:07

Third Maximum Number的相关文章

The maximum number of cell styles was exceeded. You can define up to 4000 styles

POI操作Excel中,导出的数据不是很大时,则不会有问题,而数据很多或者比较多时, 就会报以下的错误,是由于cell styles太多create造成,故一般可以把cellstyle设置放到循环外面 报错如下: Caused by: java.lang.IllegalStateException: The maximum number of cell styles was exceeded. You can define up to 4000 styles in a .xls workbook

POJ2699 The Maximum Number of Strong Kings

Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2102   Accepted: 975 Description A tournament can be represented by a complete graph in which each vertex denotes a player and a directed edge is from vertex x to vertex y if player x beats

[LintCode] Create Maximum Number 创建最大数

Given two arrays of length m and n with digits 0-9 representing two numbers. Create the maximum number of length k <= m + n from digits of the two. The relative order of the digits from the same array must be preserved. Return an array of the k digit

Maximum number of WAL files in the pg_xlog directory (1)

Guillaume Lelarge: Hi, As part of our monitoring work for our customers, we stumbled upon an issue with our customers' servers who have a wal_keep_segments setting higher than 0. We have a monitoring script that checks the number of WAL files in the

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). 给定一个非空数组的整数,返回该数组中的第三个最大数. 如果不存在,返回最大数量. 时间复杂度必须在O(n)中. Example 1: Input: [3, 2,

(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. Examp

【42】414. Third Maximum Number

414. Third Maximum Number Description Submission Solutions Add to List Total Accepted: 20624 Total Submissions: 76932 Difficulty: Easy Contributors: ZengRed, 1337c0d3r Given a non-empty array of integers, return the third maximum number in this array

Leetcode-414 Third Maximum Number

#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] Output: 1 Explanation:

tomcat 大并发报错 Maximum number of threads (200) created for connector with address null and port 8080

1.INFO: Maximum number of threads (200) created for connector with address null and port 8091 说明:最大线程数错误 解决方案: 使用线程池,用较少的线程处理较多的访问,可以提高tomcat处理请求的能力.使用方式: 首先.打开/conf/server.xml,增加 [html] view plain copy print? <Executor name="tomcatThreadPool"

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] Output: 1 Explanation: The third maximum is 1. Examp