We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
给定一个循环数组 nums ( nums[nums.length - 1] 的下一个元素是 nums[0] ),返回 nums 中每个元素的 下一个更大元素 。
数字 x 的 下一个更大的元素 是按数组遍历顺序,这个数字之后的第一个比它更大的数,这意味着你应该循环地搜索它的下一个更大的数。如果不存在,则输出 -1 。
/** * @param {number[]} nums * @return {number[]} */ let nextGreaterElements = function (nums) { const len = nums.length; let stack = []; let res = Array(len).fill(-1); for (let i = 0; i < len * 2; i++) { while ( stack.length && nums[i % len] > nums[stack[stack.length - 1]] ) { const index = stack.pop(); res[index] = nums[i % len]; } stack.push(i % len); } return res; };
The text was updated successfully, but these errors were encountered:
No branches or pull requests
来源:力扣第503题
题目描述:
给定一个循环数组 nums ( nums[nums.length - 1] 的下一个元素是 nums[0] ),返回 nums 中每个元素的 下一个更大元素 。
数字 x 的 下一个更大的元素 是按数组遍历顺序,这个数字之后的第一个比它更大的数,这意味着你应该循环地搜索它的下一个更大的数。如果不存在,则输出 -1 。
解题思路:
The text was updated successfully, but these errors were encountered: