算法模板之单调栈【java】

单调栈:在一维数组中找第一个满足某种条件的数

单调栈适用于在一维数组中找第一个满足某种条件的数的场景,包括但不限于:

  • 找到数组中每个数【左侧】第一个【大于】它的数;
  • 找到数组中每个数【左侧】第一个【大于或等于】它的数;
  • 找到数组中每个数【左侧】第一个【小于】它的数;
  • 找到数组中每个数【左侧】第一个【小于或等于】它的数;
  • 找到数组中每个数【右侧】第一个【大于】它的数;
  • 找到数组中每个数【右侧】第一个【大于或等于】它的数;
  • 找到数组中每个数【右侧】第一个【小于】它的数;
  • 找到数组中每个数【右侧】第一个【小于或等于】它的数;

找到数组中每个数【左侧】第一个【大于】它的数

class Solution {
    public int[] fun(int[] nums) {
        int[] res = new int[nums.length];
        Deque<Integer> s = new ArrayDeque<>();
        for(int i=0; i<nums.length; i++){
            while(!s.isEmpty() && nums[s.peek()] <= nums[i]){
                s.pop();
            }
            res[i] = s.isEmpty()? -1 : s.peek();
            s.push(i);
        }
        return res;
    }
}

找到数组中每个数【左侧】第一个【大于或等于】它的数

class Solution {
    public int[] fun(int[] nums) {
        int[] res = new int[nums.length];
        Deque<Integer> s = new ArrayDeque<>();
        for(int i=0; i<nums.length; i++){
            while(!s.isEmpty() && nums[s.peek()] < nums[i]){
                s.pop();
            }
            res[i] = s.isEmpty()? -1 : s.peek();
            s.push(i);
        }
        return res;
    }
}

找到数组中每个数【左侧】第一个【小于】它的数

class Solution {
    public int[] fun(int[] nums) {
        int[] res = new int[nums.length];
        Deque<Integer> s = new ArrayDeque<>();
        for(int i=0; i<nums.length; i++){
            while(!s.isEmpty() && nums[s.peek()] >= nums[i]){
                s.pop();
            }
            res[i] = s.isEmpty()? -1 : s.peek();
            s.push(i);
        }
        return res;
    }
}

找到数组中每个数【左侧】第一个【小于或等于】它的数

class Solution {
    public int[] fun(int[] nums) {
        int[] res = new int[nums.length];
        Deque<Integer> s = new ArrayDeque<>();
        for(int i=0; i<nums.length; i++){
            while(!s.isEmpty() && nums[s.peek()] > nums[i]){
                s.pop();
            }
            res[i] = s.isEmpty()? -1 : s.peek();
            s.push(i);
        }
        return res;
    }
}

找到数组中每个数【右侧】第一个【大于】它的数

class Solution {
    public int[] fun(int[] nums) {
        int[] res = new int[nums.length];
        Deque<Integer> s = new ArrayDeque<>();
        for(int i=nums.length-1; i>=0; i--){
            while(!s.isEmpty() && nums[s.peek()] <= nums[i]){
                s.pop();
            }
            res[i] = s.isEmpty()? -1 : s.peek();
            s.push(i);
        }
        return res;
    }
}

找到数组中每个数【右侧】第一个【大于或等于】它的数

class Solution {
    public int[] fun(int[] nums) {
        int[] res = new int[nums.length];
        Deque<Integer> s = new ArrayDeque<>();
        for(int i=nums.length-1; i>=0; i--){
            while(!s.isEmpty() && nums[s.peek()] < nums[i]){
                s.pop();
            }
            res[i] = s.isEmpty()? -1 : s.peek();
            s.push(i);
        }
        return res;
    }
}

找到数组中每个数【右侧】第一个【小于】它的数

class Solution {
    public int[] fun(int[] nums) {
        int[] res = new int[nums.length];
        Deque<Integer> s = new ArrayDeque<>();
        for(int i=nums.length-1; i>=0; i--){
            while(!s.isEmpty() && nums[s.peek()] >= nums[i]){
                s.pop();
            }
            res[i] = s.isEmpty()? -1 : s.peek();
            s.push(i);
        }
        return res;
    }
}

找到数组中每个数【右侧】第一个【小于或等于】它的数

class Solution {
    public int[] fun(int[] nums) {
        int[] res = new int[nums.length];
        Deque<Integer> s = new ArrayDeque<>();
        for(int i=nums.length-1; i>=0; i--){
            while(!s.isEmpty() && nums[s.peek()] > nums[i]){
                s.pop();
            }
            res[i] = s.isEmpty()? -1 : s.peek();
            s.push(i);
        }
        return res;
    }
}

同时找到数组中每个数【左侧】第一个【大于】它的数和【右侧】第一个【大于】它的数

class Solution {
    public void fun(int[] nums) {
        int n = nums.length;
        int[] left = new int[n];
        Arrays.fill(left, -1);
        int[] right = new int[n];
        Arrays.fill(right, -1);
        Deque<Integer> s= new ArrayDeque<>();
        for (int i = 0; i < n; i++) {
            while (!s.isEmpty() && nums[s.peek()] <= nums[i]) {
                right[s.pop()] = i;
            }
            if (!s.isEmpty()) {
                left[i] = s.peek();
            }
            s.push(i);
        }
    }
}
  • 6
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值