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 ...
随机推荐
- knockoutJS学习笔记06:ko数组与模板绑定
前面已经介绍了基本的绑定和模板相关知识,接下来就看ko里的数组和模板绑定,数组和模板绑定应该是实际项目中用得比较多的,ko提供了很好的支持. 一.observaleArray 前面的监控属性都是单个对 ...
- 您还在招聘网上海量投简历然后等面试机会吗?那你已经OUT了。
工作也可以来找我们.不行看完这篇. 从毕业到现在,换了2次工作.每次都在为招工组烦恼.找工作这个问题,不管是应届生还是职场老手.都面临一个问题就是找工作的平台.纵观目前的找工作的形式,主流的不外乎就两 ...
- resin启动报错:guava-15.0.jar!/META-INF/beans.xml:5: <beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"> is an unexpected top-level tag. 异常
项目完成,经过本地的测试,最后在部署的时候,发现服务器resin启动失败,报错信息如下:
- Asp.Net MVC<八>:View的呈现
ActionResult 原则上任何类型的响应都可以利用当前的HttpResponse来完成.但是MVC中我们一般将针对请求的响应实现在一个ActionResult对象中. public abstra ...
- js判断浏览器类型以及浏览器版本
判断浏览器类型: if navigator.userAgent.indexOf(”MSIE”)>0) {} //判断是否IE浏览器 if(isFirefox=navigator.userAg ...
- Leetcode 216. Combination Sum III
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
- 使用IntelliJ IDEA 配置Maven(入门)
1. 下载Maven 官方地址:http://maven.apache.org/download.cgi 解压并新建一个本地仓库文件夹 2.配置本地仓库路径 3.配置maven环境变量 ...
- 调用mybatis generator已经生成好的dao来查询例子
package com.cib.xj.controller; import java.util.List; import javax.annotation.Resource; import org.s ...
- strlen()和sizeof()求数组长度
在字符常量和字符串常量的博文里有提: 求字符串数组的长度 标准库函数strlen(s)可以返回字符串s的长度,在头文件<string.h>里. strlen(s)的判断长度的依据是(s[i ...
- Git------Win7系统使用TortoiseGit
转载: https://my.oschina.net/longxuu/blog/141699?p=1 此步可以省略 1.点击TortoiseGit->PuTTygen 2.点击"Gen ...