LeetCode 503. 下一个更大元素 II
class Solution { public int[] nextGreaterElements(int[] nums) { //结果集 int len = nums.length; int[] res = new int[len]; Arrays.fill(res,-1); //栈,保存下标 Stack<Integer> stack = new Stack<>(); for(int i = 0; i< len*2;i++){ while(!stack.isEmpty() && nums[i % len] > nums[stack.peek()]){ res[stack.pop()] = nums[i % len]; } stack.push(i % len); } return res; } }