自学内容网 自学内容网

算法模板之单调栈【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);
        }
    }
}

原文地址:https://blog.csdn.net/WuZian_CSU/article/details/140507294

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!