题解 | #单调栈#
单调栈
https://www.nowcoder.com/practice/ae25fb47d34144a08a0f8ff67e8e7fb5
import java.util.*; public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param nums int一维数组 * @return int二维数组 */ public int[][] foundMonotoneStack (int[] nums) { int[][] res = new int[nums.length][2]; res[0][0] = -1; res[nums.length - 1][1] = -1; for (int i = 1; i < nums.length; i++) { int j = i - 1; while (j != -1 && nums[j] >= nums[i]) { j = res[j][0]; } res[i][0] = j; } for (int i = nums.length - 2; i >= 0; i--) { int j = i + 1; while (j != -1 && nums[j] >= nums[i]) { j = res[j][1]; } res[i][1] = j; } return res; } }