mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 16:54:50 +08:00
添加(0739.每日温度.md):增加typescript版本
This commit is contained in:
@ -371,6 +371,32 @@ var dailyTemperatures = function(temperatures) {
|
|||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
|
TypeScript:
|
||||||
|
|
||||||
|
> 精简版:
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
function dailyTemperatures(temperatures: number[]): number[] {
|
||||||
|
const length: number = temperatures.length;
|
||||||
|
const stack: number[] = [];
|
||||||
|
const resArr: number[] = new Array(length).fill(0);
|
||||||
|
stack.push(0);
|
||||||
|
for (let i = 1; i < length; i++) {
|
||||||
|
let top = stack[stack.length - 1];
|
||||||
|
while (
|
||||||
|
stack.length > 0 &&
|
||||||
|
temperatures[top] < temperatures[i]
|
||||||
|
) {
|
||||||
|
resArr[top] = i - top;
|
||||||
|
stack.pop();
|
||||||
|
top = stack[stack.length - 1];
|
||||||
|
}
|
||||||
|
stack.push(i);
|
||||||
|
}
|
||||||
|
return resArr;
|
||||||
|
};
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user