-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path739.每日温度.js
More file actions
30 lines (29 loc) · 840 Bytes
/
739.每日温度.js
File metadata and controls
30 lines (29 loc) · 840 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/*
* @lc app=leetcode.cn id=739 lang=javascript
*
* [739] 每日温度
*/
// @lc code=start
/**
* @param {number[]} temperatures
* @return {number[]}
*/
var dailyTemperatures = function (temperatures) {
const stack = []; // 栈
const res = new Array(temperatures.length).fill(0);
for (let i = 0; i < temperatures.length; i++) {
// 当栈内有元素时 && 当前温度是否大于栈顶元素,【更新结果数组,栈顶元素出栈】
while (
stack.length > 0 &&
temperatures[i] > temperatures[stack[stack.length - 1]]
) {
res[stack[stack.length - 1]] = i - stack[stack.length - 1];
stack.pop();
}
stack.push(i); // 将当前元素入栈
}
return res;
};
const temperatures = [73, 74, 75, 71, 69, 72, 76, 73];
console.log(dailyTemperatures(temperatures));
// @lc code=end