【LeetCode】單調棧相關練習總結

題目和解析持續補充中…

496. 下一個更大元素 I

class Solution {
    public int[] nextGreaterElement(int[] nums1, int[] nums2) {
        Stack<Integer> stack = new Stack<>();
        Map<Integer,Integer> map = new HashMap<>();
        for(int i = 0;i<nums2.length;i++){
            while(!stack.isEmpty() && stack.peek()<nums2[i]){
                map.put(stack.pop(),nums2[i]);
            }
            stack.push(nums2[i]);
        }
        int[] res = new int[nums1.length];
        for(int i = 0;i<nums1.length;i++){
            res[i] = map.getOrDefault(nums1[i],-1);
        }
        return res;
    }
}

581. 最短無序連續子數組

class Solution {
    public int findUnsortedSubarray(int[] nums) {
        int left = nums.length;
        int right = 0;
        Stack<Integer> stack = new Stack<>();
        for(int i = 0;i<nums.length;i++){
            while(!stack.isEmpty()&&nums[stack.peek()]>nums[i]){
                left = Math.min(left,stack.pop());
            }
            stack.push(i);
        }
        stack.clear();
        for(int i = nums.length-1;i>=0;i--){
            while(!stack.isEmpty()&&nums[stack.peek()]<nums[i]){
                right = Math.max(right,stack.pop());
            }
            stack.push(i);
        }
        return right-left>0?right-left+1:0;
    }
}

402. 移掉K位數字

class Solution {
    public String removeKdigits(String num, int k) {
        StringBuilder res = new StringBuilder();
        int n = num.length(), m = n - k;
        for (char c : num.toCharArray()) {
            while (k != 0 && res.length() != 0 && res.charAt(res.length() - 1) > c) {
                res = res.deleteCharAt(res.length() - 1);
                --k;
            }
            res.append(c);
        }
        res = res.delete(m, res.length());
        // 去除前導0, 如10200,k = 1
        while (res.length() != 0 && res.charAt(0) == '0') {
            res = res.deleteCharAt(0);
        }
        return res.length() == 0 ? "0" : res.toString();
    }
}

739. 每日溫度

class Solution {
    public int[] dailyTemperatures(int[] T) {
        int[] res = new int[T.length];
        Stack<Integer> stack = new Stack<>();
        for(int i = 0;i<T.length;i++){
            while(!stack.isEmpty()&&T[stack.peek()]<T[i]){
                int curr = stack.pop();
                res[curr] = i - curr;
            }
            stack.push(i);
        }
        return res;
    }
}

84. 柱狀圖中最大的矩形

class Solution {
    public int largestRectangleArea(int[] heights) {
        int len = heights.length;
        Stack<Integer> s = new Stack<>();
        int maxArea = 0;
        for (int i = 0; i <= len; i++){
            int h = (i == len ? 0 : heights[i]);
            if (s.isEmpty() || h >= heights[s.peek()]) {
                s.push(i);
            } else {
                int tp = s.pop();
                maxArea = Math.max(maxArea, heights[tp] * (s.isEmpty() ? i : i - 1 - s.peek()));
                i--;
            }
        }
        return maxArea;
    }
}

42. 接雨水

class Solution {
    public int trap(int[] height) {
        Stack<Integer> stack = new Stack<>();
        int sum = 0;
        for(int i = 0;i<height.length;i++){
            while(!stack.isEmpty()&&height[stack.peek()]<height[i]){
                int currHeight = stack.pop();
                while(!stack.isEmpty()&&height[stack.peek()]==height[currHeight]){
                    stack.pop();
                }
                if(!stack.isEmpty()){
                    sum += (Math.min(height[stack.peek()],height[i]) - height[currHeight]) * (i-stack.peek()-1);
                }
            }
            stack.push(i);
        }
        return sum;
    }
}

你知道的越多,你不知道的越多。
有道無術,術尚可求,有術無道,止於術。
如有其它問題,歡迎大家留言,我們一起討論,一起學習,一起進步

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章