成人无码视频,亚洲精品久久久久av无码,午夜精品久久久久久毛片,亚洲 中文字幕 日韩 无码

資訊專欄INFORMATION COLUMN

[LeetCode] Third Maximum Number

red_bricks / 1776人閱讀

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

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.

Solution
class Solution {
    public int thirdMax(int[] nums) {
        if (nums == null || nums.length == 0) return -1;
        int first = Integer.MIN_VALUE, second = first, third = first;
        boolean firstExists = false, secondExists = false, thirdExists = false;
        for (int num: nums) {
            if (num >= first) {
                if (!firstExists) {
                    first = num;
                    firstExists = true;
                } else if (num == first) {
                    continue;
                } else {
                    if (secondExists) {
                        third = second;
                        thirdExists = true;
                    } else {
                        secondExists = true;
                    }
                    second = first;
                    first = num;
                }
            } else if (num >= second) {
                if (!secondExists) {
                    second = num;
                    secondExists = true;
                } else if (num == second) {
                    continue;
                } else {                    
                    if (!thirdExists) {
                        thirdExists = true;
                    }                   
                    third = second;
                    second = num;
                }
            } else if (num >= third) {
                thirdExists = true;
                third = num;
            }
        }
        System.out.println(first);
        System.out.println(second);
        System.out.println(third);

        return thirdExists == false ? first : third;
    }
}

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://m.hztianpu.com/yun/76771.html

相關(guān)文章

  • leetcode 414 Third Maximum Number

    摘要:題目詳情給定一個(gè)輸入的數(shù)組,我們需要找出這個(gè)數(shù)組中第三大的數(shù),而且時(shí)間復(fù)雜度必須是如果不存在第三大的數(shù),則返回最大的數(shù)。思路因?yàn)闀r(shí)間復(fù)雜度為,所以預(yù)排序是不可以的我們一定要在一次遍歷結(jié)束后就找到這個(gè)第三大的值。 題目詳情 Given a non-empty array of integers, return the third maximum number in this array....

    anquan 評(píng)論0 收藏0
  • leetcode 部分解答索引(持續(xù)更新~)

    摘要:前言從開始寫相關(guān)的博客到現(xiàn)在也蠻多篇了。而且當(dāng)時(shí)也沒有按順序?qū)懍F(xiàn)在翻起來覺得蠻亂的。可能大家看著也非常不方便。所以在這里做個(gè)索引嘻嘻。順序整理更新更新更新更新更新更新更新更新更新更新更新更新更新更新更新更新 前言 從開始寫leetcode相關(guān)的博客到現(xiàn)在也蠻多篇了。而且當(dāng)時(shí)也沒有按順序?qū)憽F(xiàn)在翻起來覺得蠻亂的??赡艽蠹铱粗卜浅2环奖?。所以在這里做個(gè)索引嘻嘻。 順序整理 1~50 1...

    leo108 評(píng)論0 收藏0
  • 前端 | 每天一個(gè) LeetCode

    摘要:在線網(wǎng)站地址我的微信公眾號(hào)完整題目列表從年月日起,每天更新一題,順序從易到難,目前已更新個(gè)題。這是項(xiàng)目地址歡迎一起交流學(xué)習(xí)。 這篇文章記錄我練習(xí)的 LeetCode 題目,語言 JavaScript。 在線網(wǎng)站:https://cattle.w3fun.com GitHub 地址:https://github.com/swpuLeo/ca...我的微信公眾號(hào): showImg(htt...

    張漢慶 評(píng)論0 收藏0
  • LeetCode 攻略 - 2019 年 7 月下半月匯總(100 題攻略)

    摘要:月下半旬攻略道題,目前已攻略題。目前簡(jiǎn)單難度攻略已經(jīng)到題,所以后面會(huì)調(diào)整自己,在刷算法與數(shù)據(jù)結(jié)構(gòu)的同時(shí),攻略中等難度的題目。 Create by jsliang on 2019-07-30 16:15:37 Recently revised in 2019-07-30 17:04:20 7 月下半旬攻略 45 道題,目前已攻略 100 題。 一 目錄 不折騰的前端,和咸魚有什么區(qū)別...

    tain335 評(píng)論0 收藏0
  • LeetCode[321] Create Maximum Number

    摘要:算法復(fù)雜度思路貪心算法,先能組成的數(shù)的組合,然后針對(duì)每一個(gè)組合,考慮每一個(gè)數(shù)組能夠組成的最大的位或者位數(shù)。對(duì)不同組合生成的最大數(shù)進(jìn)行比較,得到所能得到的最大的值。代碼的方法去找這個(gè)數(shù)。 LeetCode[321] Create Maximum Number Given two arrays of length m and n with digits 0-9 representing ...

    UsherChen 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

閱讀需要支付1元查看
<