Skip to content

Commit 10ad541

Browse files
committed
添加(0503.下一个更大元素II.md):增加typescript版本
1 parent e07a3ca commit 10ad541

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

problems/0503.下一个更大元素II.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,5 +182,31 @@ var nextGreaterElements = function (nums) {
182182
return res;
183183
};
184184
```
185+
TypeScript:
186+
187+
```typescript
188+
function nextGreaterElements(nums: number[]): number[] {
189+
const length: number = nums.length;
190+
const stack: number[] = [];
191+
stack.push(0);
192+
const resArr: number[] = new Array(length).fill(-1);
193+
for (let i = 1; i < length * 2; i++) {
194+
const index = i % length;
195+
let top = stack[stack.length - 1];
196+
while (stack.length > 0 && nums[top] < nums[index]) {
197+
resArr[top] = nums[index];
198+
stack.pop();
199+
top = stack[stack.length - 1];
200+
}
201+
if (i < length) {
202+
stack.push(i);
203+
}
204+
}
205+
return resArr;
206+
};
207+
```
208+
209+
210+
185211
-----------------------
186212
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>

0 commit comments

Comments
 (0)