public int[] findMax(int[] nums) {
int n = nums.length;
int[] res = new int[n];
Deque<Integer> stack = new ArrayDeque<>();
for (int i = 0; i < n; i++) {
while (!stack.isEmpty() && nums[i] > nums[stack.peek()]) {
res[stack.pop()] = nums[i];
}
stack.push(i);
}
while (!stack.isEmpty()) {
res[stack.pop()] = -1;
}
return res;
}
数组找右边第一个比它大的数 没有输出-1
最新推荐文章于 2020-10-21 21:22:35 发布