Merge pull request #1384 from xiaofei-2020/mono03

添加(0503.下一个更大元素II.md):增加typescript版本
This commit is contained in:
程序员Carl
2022-06-20 11:27:53 +08:00
committed by GitHub

View File

@ -182,5 +182,31 @@ var nextGreaterElements = function (nums) {
return res; 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> <div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>