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

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.

题意寻找一个数组中第三大的数,如果不存在返回最大的数。直接的做法就是排序,查找第三个即可,C++实现如下
int thirdMax(vector<int>& nums) {
        sort(nums.begin(),nums.end());
        int count=1;
        for(int i=nums.size()-1;i>0;i--)
        {
            if(nums[i]!=nums[i-1])
                count++;
            if(count==3)
                return nums[--i];
        }
        return nums[nums.size()-1];
    }

 也可以利用set集合的有序性和唯一性,C++实现如下

 int thirdMax(vector<int>& nums) {
        set<int> t_set;
        for(auto &i:nums)
            t_set.insert(i);
        auto r_iter=t_set.rbegin();
        if(t_set.size()<3)
            return *r_iter;
        r_iter++;
        r_iter++;
        return *r_iter;
    }        
 
时间: 2024-10-19 08:28:18

(leetcode题解)Third Maximum Number的相关文章

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

[LeetCode 题解]:Palindrome Number

前言   [LeetCode 题解]系列传送门:  http://www.cnblogs.com/double-win/category/573499.html   1.题目描述 Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could negative integers be palindromes? (ie, -1) If you are thinking of c

Leetcode 321: 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 + nfrom digits of the two. The relative order of the digits from the same array must be preserved. Return an array of the k digits

(leetcode题解)Maximum Subarray

Find the contiguous subarray within an array (containing at least one number) which has the largest sum. For example, given the array [-2,1,-3,4,-1,2,1,-5,4],the contiguous subarray [4,-1,2,1] has the largest sum = 6. 这是一道经典的题目,给定一个数组求和最大的子数组.算法导论对这道

LeetCode题解 #9 Palindrome Number

题目大意:给定一个整型(即int),判断其是否为回文数 首先负数肯定不是回文了,只要判断正数就好. 将数字不断%10/10一个个取出来,放到一个数组中.然后再从数组两头开始往中间比较,有不等的马上返回false就好. public static boolean isPalindrome(int x) { if(x<0) return false; int [] data = new int [13]; int count = 0; int temp = 0; while(x>0){ data[

LeetCode Third Maximum Number

原题链接在这里:https://leetcode.com/problems/third-maximum-number/#/description 题目: 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). Exa

[LeetCode OJ] Max Points on a Line—Given n points on a 2D plane, find the maximum number of points that lie on the same straight line.

//定义二维平面上的点struct Point { int x; int y; Point(int a=0, int b=0):x(a),y(b){} }; bool operator==(const Point& left, const Point& right) { return (left.x==right.x && left.y==right.y); } //求两个点连接成的直线所对应的斜率 double line_equation(const Point&

[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

【leetcode】1072. Flip Columns For Maximum Number of Equal Rows

题目如下: Given a matrix consisting of 0s and 1s, we may choose any number of columns in the matrix and flip every cell in that column.  Flipping a cell changes the value of that cell from 0 to 1 or from 1 to 0. Return the maximum number of rows that hav