Problem
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 warmer temperature. If there is no future day for which this is possible, put 0 instead.
For example, given the list of temperatures T = [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].
Solutionclass Solution { public int[] dailyTemperatures(int[] T) { //use stack to save prev indexes int n = T.length; int[] res = new int[n]; Dequestack = new ArrayDeque<>(); for (int i = 0; i < n; i++) { while (!stack.isEmpty() && T[stack.peek()] < T[i]) { int preIndex = stack.pop(); res[preIndex] = i-preIndex; } stack.push(i); } return res; } }
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://m.specialneedsforspecialkids.com/yun/72535.html
摘要:提示氣溫列表長(zhǎng)度的范圍是。第二次遍歷棧頂對(duì)應(yīng)索引的溫度索引出棧此時(shí)棧為空,。索引入棧,第三次遍歷棧頂對(duì)應(yīng)索引的溫度滿足要求當(dāng)前索引入棧。每個(gè)氣溫的值的均為華氏度,都是在范圍內(nèi)的整數(shù)。 題目: 根據(jù)每日 氣溫 列表,請(qǐng)重新生成一個(gè)列表,對(duì)應(yīng)位置的輸入是你需要再等待多久溫度才會(huì)升高超過(guò)該日的天數(shù)。如果之后都不會(huì)升高,請(qǐng)?jiān)谠撐恢糜?0 來(lái)代替。 例如,給定一個(gè)列表 temperatures ...
Problem 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 whi...
摘要:效果預(yù)覽按下右側(cè)的點(diǎn)擊預(yù)覽按鈕可以在當(dāng)前頁(yè)面預(yù)覽,點(diǎn)擊鏈接可以全屏預(yù)覽??山换ヒ曨l此視頻是可以交互的,你可以隨時(shí)暫停視頻,編輯視頻中的代碼。 showImg(https://segmentfault.com/img/bVbeUYJ?w=400&h=300); 效果預(yù)覽 按下右側(cè)的點(diǎn)擊預(yù)覽按鈕可以在當(dāng)前頁(yè)面預(yù)覽,點(diǎn)擊鏈接可以全屏預(yù)覽。 https://codepen.io/comehop...
摘要:效果預(yù)覽按下右側(cè)的點(diǎn)擊預(yù)覽按鈕可以在當(dāng)前頁(yè)面預(yù)覽,點(diǎn)擊鏈接可以全屏預(yù)覽??山换ヒ曨l此視頻是可以交互的,你可以隨時(shí)暫停視頻,編輯視頻中的代碼。 showImg(https://segmentfault.com/img/bVbeUYJ?w=400&h=300); 效果預(yù)覽 按下右側(cè)的點(diǎn)擊預(yù)覽按鈕可以在當(dāng)前頁(yè)面預(yù)覽,點(diǎn)擊鏈接可以全屏預(yù)覽。 https://codepen.io/comehop...
閱讀 1728·2021-10-18 13:34
閱讀 3919·2021-09-08 10:42
閱讀 1562·2021-09-02 09:56
閱讀 1613·2019-08-30 15:54
閱讀 3135·2019-08-29 18:44
閱讀 3307·2019-08-26 18:37
閱讀 2223·2019-08-26 12:13
閱讀 462·2019-08-26 10:20