public int[] nextGreaterElements(int[] nums) {
int[] res = new int[nums.length];
int n = nums.length;
Arrays.fill(res, -1);
Deque<Integer> stack = new ArrayDeque<>();
for (int i = 0; i < 2 * n; i++) {
int x = nums[i % n];
while (!stack.isEmpty() && nums[stack.peek()] < x) {
res[stack.pop()] = x;
}
if (i < n) stack.push(i);
}
return res;
}
503. Next Greater Element II
最新推荐文章于 2022-08-22 21:35:39 发布