mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 03:34:02 +08:00
修改 0503.下一个更大元素II javascript 版本
This commit is contained in:
@ -166,20 +166,19 @@ JavaScript:
|
||||
* @return {number[]}
|
||||
*/
|
||||
var nextGreaterElements = function (nums) {
|
||||
// let map = new Map();
|
||||
const len = nums.length;
|
||||
let stack = [];
|
||||
let res = new Array(nums.length).fill(-1);
|
||||
for (let i = 0; i < nums.length * 2; i++) {
|
||||
let res = Array(len).fill(-1);
|
||||
for (let i = 0; i < len * 2; i++) {
|
||||
while (
|
||||
stack.length &&
|
||||
nums[i % nums.length] > nums[stack[stack.length - 1]]
|
||||
nums[i % len] > nums[stack[stack.length - 1]]
|
||||
) {
|
||||
let index = stack.pop();
|
||||
res[index] = nums[i % nums.length];
|
||||
const index = stack.pop();
|
||||
res[index] = nums[i % len];
|
||||
}
|
||||
stack.push(i % nums.length);
|
||||
stack.push(i % len);
|
||||
}
|
||||
|
||||
return res;
|
||||
};
|
||||
```
|
||||
|
Reference in New Issue
Block a user