LeetCode 414 Third Maximum Number
Problem:
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.
Summary:
找出整形数组中第三大的数,若存在第三大的数则返回该数,否则返回最大的数。
注意所谓的“大”为严格意义上的大于,大于等于的关系不成立。
Solution:
1. 首先将数组从大到小排序,然后从头遍历去掉重复数字,同时用一个指针记录当下去掉重复数字后的索引值。最后判断新数组中数字个数是否大于3即可。
class Solution {
public:
static bool descending (int i, int j) { //自定义排序函数,此处应为static,否则报错
return i > j;
} int thirdMax(vector<int>& nums) {
int len = nums.size();
sort(nums.begin(), nums.end(), descending);
//sort(nums.begin(). nums.end(), greater<int>()); int j = ;
for (int i = ; i < len; i++) {
if(nums[i] < nums[i - ]) {
nums[j] = nums[i];
j++;
}
} return j > ? nums[] : nums[];
}
};
2. 用set维护当前的三个最大值,由于set有去重和自动从小到大排序的功能,可以再size大于3时去掉最小的,即当前的第四大数。
class Solution {
public:
int thirdMax(vector<int>& nums) {
int len = nums.size();
set<int> s;
for (int i = ; i < len; i++) {
s.insert(nums[i]);
if (s.size() > ) {
s.erase(s.begin());
}
} return s.size()== ? *s.begin() : *s.rbegin();
}
};
参考:http://www.cnblogs.com/grandyang/p/5983113.html
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.
LeetCode 414 Third Maximum Number的更多相关文章
- C#版 - Leetcode 414. Third Maximum Number题解
版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...
- LeetCode 414. Third Maximum Number (第三大的数)
Given a non-empty array of integers, return the third maximum number in this array. If it does not e ...
- 【leetcode】414. Third Maximum Number
problem 414. Third Maximum Number solution 思路:用三个变量first, second, third来分别保存第一大.第二大和第三大的数,然后遍历数组. cl ...
- 【LeetCode】414. Third Maximum Number 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 替换最大值数组 使用set 三个变量 日期 题目地址 ...
- LeetCode Array Easy 414. Third Maximum Number
Description Given a non-empty array of integers, return the third maximum number in this array. If i ...
- [LeetCode] 321. Create Maximum Number 创建最大数
Given two arrays of length m and n with digits 0-9 representing two numbers. Create the maximum numb ...
- LeetCode 321. Create Maximum Number
原题链接在这里:https://leetcode.com/problems/create-maximum-number/description/ 题目: Given two arrays of len ...
- 【leetcode】1189. Maximum Number of Balloons
题目如下: Given a string text, you want to use the characters of text to form as many instances of the w ...
- [LeetCode] 414. Third Maximum Number_Easy
Given a non-empty array of integers, return the third maximum number in this array. If it does not e ...
随机推荐
- spring-boot-starter-amqp踩坑记
踩坑记录 近日在用spring boot架构一个微服务框架,服务发现与治理.发布REST接口各种轻松惬意.但是服务当设计MQ入口时,就发现遇到无数地雷,现在整理成下文,供各路大侠围观与嘲笑. 版本 当 ...
- 解决 Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in
转载 php 5个版本,5.2.5.3.5.4.5.5,怕跟不上时代,新的服务器直接上5.5,但是程序出现如下错误:Deprecated: mysql_connect(): The mysql ext ...
- bzoj 4557: [JLoi2016]侦察守卫 树归
bzoj 4557: [JLoi2016]侦察守卫 设f[x][j]表示覆盖以x为根的子树的所有应该被覆盖的节点,并且以x为根的子树向下j层全部被覆盖的最小代价. 设g[x][j]表示与x距离大于j全 ...
- gulp watch出现Error: watch null EPERM的问题解释
出现这样的问题,一般是第一次运行导致的,而且任务上有删除文件的操作. 我观察发现,只要把输出目录的文件删除,然后重新运行watch就一些ok,后者再运行一次gulp watch就一切正常.
- Javascript和HTML:
JavaScript一种直译式脚本语言,是一种动态类型.弱类型.基于原型的语言,内置支持类型.它的解释器被称为JavaScript引擎,为浏览器的一部分,广泛用于客户端的脚本语言,最早是在HTML(标 ...
- 使用Maven Assembly plugin将依赖打包进jar
一个Eclipse的工程,在pom中配置了若干依赖,需要将pom中所有的依赖全部打包进一个jar包中,可以选择的方案有maven-assembly-plugin和fatjar.以前采用fatjar进行 ...
- Spring-IOC-BeanFactory
BeanFactory BeanFactory 是 Spring 的“心脏”.它就是 Spring IoC 容器的真面目.Spring 使用 BeanFactory 来实例化.配置和管理 Bean.但 ...
- ArcGIS Server开发教程系列(8)ArcGIS API for Javascript-控件(小部件)
1. 鹰眼 OverviewMap小部件用于在其关联的主地图内较清楚的当前鸟瞰图的范围.当主地图范围变化时,鹰眼图会自动在其空间内更新范围以保持和地图的当前范围保持一致,当鹰眼图空间的地图范围 ...
- MYSQL提权方法
cve-2016-6662 MYSQL提权分析 MySQL <= 5.7.15 远程代码执行/ 提权 (0day) 5.6.33 5.5.52 Mysql分支的版本也受影响,包括: MariaD ...
- IAP
release_check_url := "https://buy.itunes.apple.com/verifyReceipt" debug_check_url := " ...