【42】414. Third Maximum Number

414. Third Maximum Number

Description Submission Solutions Add to List

  • Total Accepted: 20624
  • Total Submissions: 76932
  • Difficulty: Easy
  • Contributors: ZengRed1337c0d3r

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]

Output: 2

Explanation: The third maximum does not exist, so the maximum (2) is returned instead.

Example 3:

Input: [2, 2, 3, 1]

Output: 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.
 1 class Solution {
 2 public:
 3     int thirdMax(vector<int>& nums) {
 4         long first = LONG_MIN, second = LONG_MIN, third = LONG_MIN;//初始化要用长整型long的最小值,否则当数组中有INT_MIN存在时,程序就不知道该返回INT_MIN还是最大值first了
 5         for(int i = 0; i < nums.size(); i++){
 6             if(nums[i] > first){//update 3 个
 7                 third = second;
 8                 second = first;
 9                 first = nums[i];
10             }else if(nums[i] > second && nums[i] < first){//update second & third
11                 third = second;
12                 second = nums[i];
13             }else if(nums[i] > third && nums[i] < second){
14                 third = nums[i];
15             }
16         }
17         /*
18         if(third == INT_MIN){
19             return second == INT_MIN ? first : second;
20         }
21         return third;*/
22         return (third == LONG_MIN || third == second) ? first : third;
23     }
24 };
				
时间: 2024-10-24 21:52:58

【42】414. Third Maximum Number的相关文章

【bzoj4604】The kth maximum number

暴力 #include<algorithm> #include<iostream> #include<cstdlib> #include<cstring> #include<cstdio> #include<cmath> using namespace std; #define MAXN 50010 int a[MAXN]; int X[MAXN],Y[MAXN],W[MAXN]; int C,N,Q,L; int read() {

LeetCode:接雨水【42】

LeetCode:接雨水[42] 题目描述 给定 n 个非负整数表示每个宽度为 1 的柱子的高度图,计算按此排列的柱子,下雨之后能接多少雨水. 上面是由数组 [0,1,0,2,1,0,1,3,2,1,2,1] 表示的高度图,在这种情况下,可以接 6 个单位的雨水(蓝色部分表示雨水). 感谢 Marcos 贡献此图. 示例: 输入: [0,1,0,2,1,0,1,3,2,1,2,1] 输出: 6 题目分析 找出最高点 分别从两边往最高点遍历:如果下一个数比当前数小,说明可以接到水 Java题解 c

【LeetCode】Binary Tree Maximum Path Sum 解题报告

[题目] Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. For example: Given the below binary tree, 1 / 2 3 Return 6. [解析] 题意:在二叉树中找一条路径,使得该路径的和最大.该路径可以从二叉树任何结点开始,也可以到任何结点结束. 思路:递归求一条经过root的最大路径,这条路径可能是:

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 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

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

letecode [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

【LeetCode】Excel Sheet Column Number

题意: Related to question Excel Sheet Column Title Given a column title as appear in an Excel sheet, return its corresponding column number. For example: A -> 1 B -> 2 C -> 3 ... Z -> 26 AA -> 27 AB -> 28 思路: 其实就是个进制转换.水水就过.倒是 Python 的代码让我

【leetcode】Binary Tree Maximum Path Sum (medium)

Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. 找树的最大路径和 注意路径可以从任意点起始和结束. 我发现我真的还挺擅长树的题目的,递归不难.就是因为有个需要比较的量(最大和),所以需要再写一个函数. 因为路径可以从任意点起始和结束,所以每次递归的时候左右子树小于等于0的就可以不管了. #include <iostream> #include