前言

本文隶属于专栏《LeetCode 刷题汇总》,该专栏为笔者原创,引用请注明来源,不足和错误之处请在评论区帮忙指出,谢谢!

本专栏目录结构请见LeetCode 刷题汇总

正文

幕布

在这里插入图片描述

幕布链接

81. 搜索旋转排序数组 II

题解

官方题解

先判断 target 位于中间的,left <= target < mid,mid < target <= right

class Solution {
    public boolean search(int[] nums, int target) {
        int left= 0;
        int right= nums.length - 1;
        while (left<= right) {
            int mid = (left+ right) / 2;
            if (target == nums[mid]) {
                return true;
            } 
            if (nums[left] < nums[mid]) {
                if (target >= nums[left] && target < nums[mid]) {
                    right= mid - 1;
                } else {
                    left = mid + 1;
                }
            } else if(nums[left] == nums[mid]){
                left++;
            }else{
                if (target > nums[mid] && target <= nums[right]) {
                    left= mid + 1;
                } else {
                    right= mid - 1;
                }
            }
        }
        return false;
    }
}

82. 删除排序链表中的重复元素 II

题解

My accepted Java code

哨兵+双指针

class Solution {
    public ListNode deleteDuplicates(ListNode head) {
        if(head == null) return null;
        ListNode dummy = new ListNode(0);
        dummy.next = head;
        ListNode pre = dummy;
        ListNode cur = head;
        while(cur != null){
            while(cur.next != null && cur.val == cur.next.val){
                cur = cur.next;
            }
            if(pre.next == cur){
                pre = pre.next;
            }else{
                pre.next = cur.next;
            }
            cur = cur.next;
        }
        return dummy.next;
    }
}

83. 删除排序链表中的重复元素

题解

3 Line JAVA recursive solution

递归

class Solution {
    public ListNode deleteDuplicates(ListNode head) {
        if(head == null || head.next == null)return head;
        head.next = deleteDuplicates(head.next);
        return head.val == head.next.val ? head.next : head;
    }
}

84. 柱状图中最大的矩形

题解

官方题解

单调栈

class Solution {
    public int largestRectangleArea(int[] heights) {
        int n = heights.length;
        if(n == 1) return heights[0];
        int[] h = new int[n + 2];
        System.arraycopy(heights, 0, h, 1, n);
        Deque<Integer> stack = new ArrayDeque<>(n);
        int max = 0;
        for(int i = 0; i < n + 2; i++){
            while(!stack.isEmpty() && h[i] < h[stack.peek()]){
                int height = h[stack.pop()];
                int width = i - stack.peek() - 1;
                max = Math.max(max, width * height);
            }
            stack.push(i);
        }
        return max;
    }
}

85. 最大矩形

题解

官方题解

构造柱形图 + 求柱形图中最大的矩形

class Solution {
    public int maximalRectangle(char[][] matrix) {
        int m = matrix.length;
        if(m == 0) return 0;
        int n = matrix[0].length;
        if(n == 0) return 0;
        int[][] heights = new int[m][n];
        int max = 0;
        for(int i = 0; i < m; i++){
            for(int j = 0; j < n; j++){
                if(i == 0 && matrix[i][j] == '1'){
                    heights[i][j] = 1;
                }
                else if(matrix[i][j] == '1'){
                    heights[i][j] = heights[i - 1][j] + 1;
                }else{
                    heights[i][j] = 0;
                }
            }
            max = Math.max(max, getMaxArea(heights[i], n));
        }
        return max;
    }

    private int getMaxArea(int[] heights,int n){
        int[] h = new int[n + 2];
        System.arraycopy(heights, 0, h, 1, n);
        Deque<Integer> stack = new ArrayDeque<>();
        int max = 0;
        for(int i = 0; i < n + 2; i++){
            while(!stack.isEmpty() && h[i] < h[stack.peek()]){
                int height = h[stack.pop()];
                int width = i - stack.peek() - 1;
                max = Math.max(max, width * height);
            }
            stack.push(i);
        }
        return max;
    }
}
上一篇 下一篇