302. Smallest Rectangle Enclosing Black Pixels My Submissions QuestionEditorial Solution
2016-03-31 12:13
651 查看
An image is represented by a binary matrix with
For example, given the following image:
and
0as a white pixel and
1as 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.
For example, given the following image:
[ "0010", "0110", "0100" ]
and
x = 0,
y = 2, return
6.
public class Solution { public int minArea(char[][] image, int x, int y) { int m = image.length; int n = image[0].length; int left = bsHor(image, 0, y, 0, m, true); int right = bsHor(image, y+1, n, 0, m, false); int top = bsVer(image, 0, x, left, right, true); int bottom = bsVer(image, x+1, m, left, right, false); return (bottom - top) * (right - left); } private int bsHor(char[][] image, int i, int j, int top, int bottom, boolean leftSide) { while (i != j) { int k = top; int mid = (j - i) / 2 + i; while (k<bottom && image[k][mid] == '0') k++; if (k < bottom == leftSide) { j = mid; } else { i = mid + 1; } } return i; } private int bsVer(char[][] image, int i, int j, int left, int right, boolean upSide) { while (i != j) { int k = left; int mid = (j - i) / 2 + i; while (k<right && image[mid][k] == '0') k++; if (k < right == upSide) { j = mid; } else { i = mid + 1; } } return i; } }
相关文章推荐
- Issues about gradle version and Android Plugin Version
- Mybatis报TypeException:JDBC requires that the JdbcType must be specified for all nullable parameters.
- DruidDataSource配置
- 定义一个Map对象,遍历并打印出各元素的key和value
- OnClick与android:focusableInTouchMode="true"
- Microsoft Build 2016 Day 1 记录
- ios学习--layer的应用(设置UIView子类边框属性&字体阴影属性)
- Vue.js 综合
- UITableView的使用
- HDU 3998 Sequence(LIS+最大流-Dinic)
- iOS:Cocoa编码规范 -[译]Coding Guidelines for Cocoa
- 《iOS Human Interface Guidelines》——Slider
- requirejs 一个拆分js项目的类库
- AndroidUI设计思想:程序易用性
- php采集(phpOuery用法)
- 微软 Build 2016年开发者大会发布多项功能升级
- 百度编辑器ueditor 多图上传 文件格式不允许 后台配置项返回出错 解决方法
- 232. Implement Queue using Stacks
- [poj 1019] Number Sequence 数学 想法题
- iOS开发-环信UI集成