503. 下一个更大元素 II
class Solution {
public int[] nextGreaterElements(int[] nums) {
if (nums == null || nums.length == 0)
return new int[0];
int len = nums.length;
Deque<Integer> stack = new LinkedList<>();
int[] ans = new int[len];
Arrays.fill(ans, -1);
for (int i = 0; i < 2 * len - 1; i++) {
while (!stack.isEmpty() && nums[i % len] > nums[stack.peek()]) {
ans[stack.pop()] = nums[i % len];
}
stack.push(i % len);
}
return ans;
}
}