1. 程式人生 > >Daily Temperatures

Daily Temperatures

Given a list of daily temperatures, produce a list that, for each day in the input, tells you how many days you would have to wait until a warmer temperature. If there is no future day for which this is possible, put 0 instead.

For example, given the list temperatures = [73, 74, 75, 71, 69, 72, 76, 73]

, your output should be [1, 1, 4, 2, 1, 1, 0, 0].

Note: The length of temperatures will be in the range [1, 30000]. Each temperature will be an integer in the range [30, 100].

題目要求計算出陣列中每一個數字後面第一個比它大的數字與其之間的距離。

首先想到的第一種解法就是用雙層迴圈,對陣列中的每個數字進行遍歷,然後計算第一個比它大的值與它之間的距離即可,但是這樣做的話時間複雜度比較高,時間複雜度為O(n^2),那麼我們有沒有更快的辦法呢,答案是有的。

在這裡我們可以利用堆疊對陣列中的每一個元素進行控制,注意原陣列的數值只是對計算過程的比較起作用,我們最終的結果只與元素的下標有關,所以我們在堆疊中儲存每個數字的下標,每次只要遍歷到更大的數字的時候,這時候就要對堆疊進行一個pop的操作,這時候也正是我們記錄距離的時刻:

public class DailyTemperatures {
    public static int[] dailyTemperatures(int[] temperatures) {
        if (temperatures == null)
            return null;
        if
(temperatures.length == 0) return temperatures; int[] result = new int[temperatures.length]; Stack<Integer> stack = new Stack<>(); for (int i = 0; i < temperatures.length; i++) { while (!stack.isEmpty()) { if (temperatures[i] > temperatures[stack.peek()]) { int index = stack.pop(); result[index] = i - index; } else { break; } } stack.push(i); } return result; } public static void main(String[] args) { int[] temperatures = new int[]{73, 74, 75, 71, 69, 72, 76, 73}; System.out.println(Arrays.toString(dailyTemperatures(temperatures))); return; } }

時間複雜度為O(n),空間複雜度為O(n)。

相關推薦

[LeetCode] Daily Temperatures 日常溫度

vector des http 入棧 但是 dai for each ref stack Given a list of daily temperatures, produce a list that, for each day in the input, tells

[LeetCode] Daily Temperatures

instead break for each ide tput you stack 比較 clas Given a list of daily temperatures, produce a list that, for each day in the input, tel

739. Daily Temperatures

com bsp pty ack TP color res etc script https://leetcode.com/problems/daily-temperatures/description/ class Solution { public: vecto

739. Daily Temperatures - LeetCode

rip png uri 實現 public 題目 length top -s Question 739.?Daily Temperatures Solution 題目大意:比今天溫度還要高還需要幾天 思路:笨方法實現,每次遍歷未來幾天,比今天溫度高,就坐標減 Java實現

[LeetCode] 739. Daily Temperatures

題:https://leetcode.com/problems/daily-temperatures/description/ 題目 Given a list of daily temperatures T, return a list such that, for each day

[Leetcode 739]*還有幾天會升溫 Daily Temperatures

【題目】 Given a list of daily temperatures T, return a list such that, for each day in the input, tells you how many days you would have to wait until a

Leetcode 739. Daily Temperatures

文章作者:Tyan 部落格:noahsnail.com  |  CSDN  |  簡書 1. Description 2. Solution Version 1 class Solution {

Daily Temperatures

Given a list of daily temperatures, produce a list that, for each day in the input, tells you how many days you would have to wai

739. Daily Temperatures(單調棧)

題目:給出一個數組,對每個位置,在當前位置後面找出最近的比當前位置大的數,求出位置差。 思路:維護一個值單調遞減的棧(棧中的元素是陣列元素的下標),當前元素大於棧頂元素,將棧頂元素出棧,直到棧空或者當前元素小於棧頂元素為止。在結果陣列中記錄結果(當前下標減

【Beta】 第一次Daily Scrum Meeting

www tab width ext 第一次 cnblogs net gin ima 一、本次會議為第一次meeting會議 二、時間:20::0AM—20:50AM 地點:宿舍樓下 三、會議站立式照片 四、今日任務安排 成員 昨日任務 今日任務 林曉

【Beta】 第六次Daily Scrum Meeting

接下來 url 研究 工作 val 模塊 ref dai 團隊 【Beta】 第六次Daily Scrum Meeting 一、本次會議為第六次meeting會議 二、時間:10:00AM—10:20AM 地點:禹州樓 三、會議站立式照片 四、今日任務安

【Beta】 第七次Daily Scrum Meeting

例如 界面 scrum body 好的 logs 微信群 好玩的 一個 第七次meeting會議 【Beta】 第七次Daily Scrum Meeting 一、本次會議為第七次meeting會議 二、時間:10:00AM—10:20AM 地點:禹州樓

Daily English Study 20170624

綠色 4.0 sof have tel style this http add 音標復習 綠色:連讀; 紅色:略讀; 藍色:濁化; 橙色:弱讀 口

[daily][pcaman] pacman滾動升級跳過指定包

rep 跳過 指定 arch dex arc dai logs bsp 有時候有些包是壞的, 我們在Syu的時候,想把它跳過去. 可以在pacman.conf 中使用如下選項: IgnorePkg=linux https://wiki.archlinux.org/

[daily][centos][nginx] 在centos7使用nginx啟用對文件目錄的http訪問

mission lld 服務 nging 參考 索引 iss firewall bsp 1. 安裝nginx yum install nginx 2. 修改配置 2.1 提供目錄權限:   我需要訪問的目錄是 /home/data, 用戶是data, 所以修改如下配

[daily][archlinux] 那些懸而未絕,久久無法忘懷,搞啊搞啊搞不定,沒有辦法,讓人瘋掉,卻只能天天憋屈著忍著的問題

網易雲音樂 開機 每次 不定 linu 手動 網易 teamview kernel 1. mathematica 不能輸入中文. 2. T460s的指點桿速率不能設置默認, 每次開機手動修正. 3. 網易雲音樂使用不正常, 必須使用 --no-sandbox 命令 4

PSP DAILY的NABCD分析

完成 net 累加 作者 文字 專註 abcd 活動 成就 1) N (Need 需求) PSP Daily 解決了用戶(軟件工程課上學生)記錄例行報告、寫每周PSP表格和統計的需求。潛在用戶還有未來該課堂的學生和需要用PSP方法記錄任務完成時間和統計的學習者、軟件項目

PSP Daily——團隊項目Alpha發布

博客 朋友 豌豆莢 min 課堂 開始 對比 .pch 自動 視頻展示:優酷視頻鏈接。文案如下 PSP Daily軟件NABCD分析: 1) N (Need 需求) PSP Daily 解決了用戶(軟件工程課上學生)記錄例行報告、寫每周PSP表格和統計的需求。潛在用戶還有未

王者榮耀交流協會PSP Daily項目Postmortem結果

有效 反饋 時間處理 嚴格 請求 快速 工程 any href 王者榮耀交流協會PSP Daily項目Postmortem結果 整理:王超 設想和目標 1. 我們的軟件要解決什麽問題?是否定義得很清楚?是否對典型用戶和典型場景有清晰的描述? PSP Daily

基於NABCD評論作業-王者榮耀交流協會PSP DAILY

百度 nbsp 記錄 評論 生成 標簽 bcd str excel表 一、根據(不限於)NABCD評論作品的選題 N(Need,需求):在我知道PSP DAILY這款軟件的時候,就認為這款軟件對於學習軟件工程課的學生來說有很大的需要。對於需求來說,軟件工程課程中