LeetCode 81~85

前言

本文隶属于专栏《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;
    }
}
  • 10
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 9
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值