Smallest Rectangle Enclosing Black Pixels

Description:

An image is represented by a binary matrix with 0 as a white pixel and 1 as a black pixel. The black pixels are connected, i.e., there is only one black region. Pixels are connected horizontally and vertically. Given the location (x, y) of one of the black pixels, return the area of the smallest (axis-aligned) rectangle that encloses all black pixels.

问题描述:

图像中的黑色像素用1表示,白色像素用0表示,图像中所有的黑色像素是连通的,求最小的矩阵区域能够把图像中所有黑色像素连成一片。给了初始黑色像素点(X,Y)。

解法一:

思路:

题目想要我们做的事情是想象成两行,两列,往中间缩紧,直到含有一个黑的为止。。
即找到四个边界,以左边界为例说明,找到第一个有黑色像素的一列。。其余四个边界同理可得,找到最后一个有黑色像素的一列,找到第一个有黑色像素的一行,找到最后一个有黑色像素的一行。。
问题转换为找到第一个、最后一个、任意一个满足x条件的某个东西。。。所以用四个二分,并且写有黑色元素(或者全为白色元素的辅助函数)

Code:

代码中的if 比较啰嗦,为了便于读者理解,先不改了。。。

public class Solution {
   
    /**
     * @param image a binary matrix with '0' and '1'
     * @param x, y the location of one of the black pixels
     * @return an integer
     */
    public int minArea(char[][] image, int x, 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值