Problem: 503. 下一个更大元素 II
思路
👨🏫 参考题解
Code
class Solution {public int[] nextGreaterElements(int[] nums) {int n = nums.length;int[] res = new int[n];Arrays.fill(res,-1);Stack<Integer> stack = new Stack<>();//单调递减栈(存的是元素的数组下标)for(int i = 0; i < 2 * n; i++){while(!stack.isEmpty() && nums[i % n] > nums[stack.peek()]){res[stack.pop()] = nums[i%n];}stack.push(i % n);}return res;}
}