[LeetCode]Smallest Rectangle Enclosing Black Pixels

用的dfs,但是感觉这个方法应该不是最优的,否则这个题目不应该是hard

public class Solution {
    int up = Integer.MAX_VALUE;
    int low = Integer.MIN_VALUE;
    int left = Integer.MAX_VALUE;
    int right = Integer.MIN_VALUE;
    public int minArea(char[][] image, int x, int y) {
        boolean[][] record = new boolean[image.length][image[0].length];
        helper(image, record, x, y);
        return (low - up + 1) * (right - left + 1);
    }
    public void helper(char[][] image, boolean [][] record, int x, int y) {
        record[x][y] = true;
        up = Math.min(up, x);
        low = Math.max(low, x);
        left = Math.min(left, y);
        right = Math.max(right, y);
        if (x > 0 && image[x - 1][y] == '1' && !record[x - 1][y]) {
            helper(image, record, x - 1, y);
        }
        if (x + 1 < image.length && image[x + 1][y] == '1' && !record[x + 1][y]) {
            helper(image, record, x + 1, y);
        }
        if (y > 0 && image[x][y - 1] == '1' && !record[x][y - 1]) {
            helper(image, record, x, y - 1);
        }
        if (y + 1 < image[0].length && image[x][y + 1] == '1' && !record[x][y + 1]) {
            helper(image, record, x, y + 1);
        }
    }
}

后来看了下网上的思路, 写了一个二分搜索的

转载于:https://www.cnblogs.com/vision-love-programming/p/5009031.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值