mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 00:43:04 +08:00
Merge pull request #1384 from xiaofei-2020/mono03
添加(0503.下一个更大元素II.md):增加typescript版本
This commit is contained in:
@ -182,5 +182,31 @@ var nextGreaterElements = function (nums) {
|
||||
return res;
|
||||
};
|
||||
```
|
||||
TypeScript:
|
||||
|
||||
```typescript
|
||||
function nextGreaterElements(nums: number[]): number[] {
|
||||
const length: number = nums.length;
|
||||
const stack: number[] = [];
|
||||
stack.push(0);
|
||||
const resArr: number[] = new Array(length).fill(-1);
|
||||
for (let i = 1; i < length * 2; i++) {
|
||||
const index = i % length;
|
||||
let top = stack[stack.length - 1];
|
||||
while (stack.length > 0 && nums[top] < nums[index]) {
|
||||
resArr[top] = nums[index];
|
||||
stack.pop();
|
||||
top = stack[stack.length - 1];
|
||||
}
|
||||
if (i < length) {
|
||||
stack.push(i);
|
||||
}
|
||||
}
|
||||
return resArr;
|
||||
};
|
||||
```
|
||||
|
||||
|
||||
|
||||
-----------------------
|
||||
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>
|
||||
|
Reference in New Issue
Block a user