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 ...
随机推荐
- 年底发福利了——分享一下我的.NET软件开发资源
最近建了一个.NET软件开发资源的360网盘共享群,把收集的一些.NET软件开发资源分享给大家,也欢迎大家把好的东东分享一下. 资源主要有:开发工具.控件资源.书籍教程.网页设计.源码资源几大类,也希 ...
- JS组件系列——BootstrapTable+KnockoutJS实现增删改查解决方案(二)
前言:上篇 JS组件系列——BootstrapTable+KnockoutJS实现增删改查解决方案(一) 介绍了下knockout.js的一些基础用法,由于篇幅的关系,所以只能分成两篇,望见谅!昨天就 ...
- 新项目,WebTest
最近为了给Jumony for ASP.NET进行单元测试有点伤神,ASP.NET因为环境特殊,一直是单元测试的禁地,传统的单元测试工具由于运行在非ASP.NET环境,可谓是举步维艰.当然,微软在搞A ...
- 【BZOJ 3051】【UOJ #57】【WC 2013】平面图
http://www.lydsy.com/JudgeOnline/problem.php?id=3051 http://uoj.ac/problem/57 这道题需要平面图转对偶图,点定位,最小生成树 ...
- Asp.Net MVC<九>:OWIN,关于StartUp.cs
https://msdn.microsoft.com/zh-cn/magazine/dn451439.aspx(Katana 项目入门) 一不小心写了个WEB服务器 快刀斩乱麻之 Katana OWI ...
- offsetHeight, clientHeight与scrollHeight的区别
在网上搜了一下,结论非常笼统,讲IE从不讲版本,因此自己做了测试并上传结论.以下结论皆是在标准模式下测试通过的,没有测试quirk模式. clientHeight 大部分浏览器对 clientHe ...
- 裁剪要素出现错误 :HRESULT:0x80040239
pFeatureBuffer = pOutFeaCls.CreateFeatureBuffer(); pFeatureCursor = pOutFeaCls.Inser ...
- C#汉字字母数字正则
http://novell.me/master-diary/2014-11-15/regular-express-csharp-example.html https://msdn.microsoft. ...
- QT中使用函数指针
想仿命令行,所以定义了一个类,让一个String 对应一个 function,将两者输入list容器. 类中定义了 QString commandStr; void (MainWindow::*com ...
- 安全测试及B/S C/S安全性比较
一.用户认证安全的测试要考虑问题: 1. 明确区分系统中不同用户权限 2. 系统中会不会出现用户冲突 3. 系统会不会因用户的权限的改变造成混乱 4. ...