diff --git a/problems/0503.下一个更大元素II.md b/problems/0503.下一个更大元素II.md index ace4d40b..33807d26 100644 --- a/problems/0503.下一个更大元素II.md +++ b/problems/0503.下一个更大元素II.md @@ -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; +}; +``` + + + -----------------------