Merge branch 'master' of github.com:youngyangyang04/leetcode-master

This commit is contained in:
programmercarl
2022-06-27 09:59:01 +08:00
35 changed files with 1205 additions and 83 deletions

View File

@@ -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>