class Solution {
public int[] nextGreaterElements(int[] nums) {
Stack<Integer> stack=new Stack<>();
int n=nums.length;
int[] res=new int[n];
for(int i=2*n-1;i>=0;i--){
while(!stack.empty()&&nums[i%n]>=stack.peek()){
stack.pop();
}
res[i%n]=stack.empty()? -1:stack.peek();
stack.push(nums[i%n]);
}
return res;
}
}
单调栈解决循环数组的下一个更大元素
最新推荐文章于 2021-07-09 16:51:37 发布