Smallest Rectangle Enclosing Black Pixels
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.
For example, given the following image:
[
"0010",
"0110",
"0100"
]
and x = 0
, y = 2
,
Return 6
.
这题既可以用DFS,也可以二分查找,因为题目中明确说了,只有一块黑色区域。
代码来自:http://www.cnblogs.com/yrbbest/p/5050022.html
public class Solution {
public int minArea(char[][] image, int x, int y) {
if(image == null || image.length == ) {
return ;
}
int rowNum = image.length, colNum = image[].length;
int left = binarySearch(image, , y, , rowNum, true, true);
int right = binarySearch(image, y + , colNum, , rowNum, true, false);
int top = binarySearch(image, , x, left, right, false, true);
int bot = binarySearch(image, x + , rowNum, left, right, false, false); return (right - left) * (bot - top);
} private int binarySearch(char[][] image, int lo, int hi, int min, int max, boolean searchHorizontal, boolean searchLo) {
while(lo < hi) {
int mid = lo + (hi - lo) / ;
boolean hasBlackPixel = false;
for(int i = min; i < max; i++) {
if((searchHorizontal ? image[i][mid] : image[mid][i]) == '') {
hasBlackPixel = true;
break;
}
}
if(hasBlackPixel == searchLo) {
hi = mid;
} else {
lo = mid + ;
}
}
return lo;
}
}
Smallest Rectangle Enclosing Black Pixels的更多相关文章
- [LeetCode] Smallest Rectangle Enclosing Black Pixels 包含黑像素的最小矩阵
An image is represented by a binary matrix with 0 as a white pixel and 1 as a black pixel. The black ...
- LeetCode Smallest Rectangle Enclosing Black Pixels
原题链接在这里:https://leetcode.com/problems/smallest-rectangle-enclosing-black-pixels/ 题目: An image is rep ...
- 302. Smallest Rectangle Enclosing Black Pixels
题目: An image is represented by a binary matrix with 0 as a white pixel and 1 as a black pixel. The b ...
- [Locked] Smallest Rectangle Enclosing Black Pixels
An image is represented by a binary matrix with 0 as a white pixel and 1 as a black pixel. The black ...
- Smallest Rectangle Enclosing Black Pixels 解答
Question An image is represented by a binary matrix with 0 as a white pixel and 1 as a black pixel. ...
- [Swift]LeetCode302. 包含黑色像素的最小矩形 $ Smallest Rectangle Enclosing Black Pixels
An image is represented by a binary matrix with 0 as a white pixel and 1 as a black pixel. The black ...
- 【leetcode】302.Smallest Rectangle Enclosing Black Pixels
原题 An image is represented by a binary matrix with 0 as a white pixel and 1 as a black pixel. The bl ...
- LC 302. Smallest Rectangle Enclosing Black Pixels【lock, hard】
An image is represented by a binary matrix with 0 as a white pixel and 1 as a black pixel. The black ...
- LeetCode All in One 题目讲解汇总(持续更新中...)
终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...
随机推荐
- JSP-Servlet的工作流程
Servlet基础 1.Servlet概述 JSP的前身就是Servlet.Servlet就是在服务器端运行的一段小程序.一个Servlet就是一个Java类,并且可以通过“请求-响应”编程模型来访问 ...
- tamper绕WAF小结
sqlmap有一些非常好的脚本,在如下的地址中你能够发现它们.使用svn检查 https://svn.sqlmap.org/sqlmap/trunk/sqlmap sqlmap-dev 事实上,脚本的 ...
- iOS版本控制git小结--yoowei
# 显示隐藏文件 defaults write com.apple.finder AppleShowAllFiles Yes && killall Finder # 不显示隐藏文件 d ...
- 浅谈T-SQL中的联接查询
引言 平时开发时,经常会使用数据库进行增删改查,免不了会涉及多表联接.今天就简单的记录下T-SQL下的联接操作. 联接类型及其介绍 在T-SQL中联接操作使用的是JOIN表运算符.联接有三种基本的类型 ...
- logrotate关于日志轮询和分割
如果你的是源码包安装的服务,那么对于Linux服务器上的一些服务在运行当中产生的日志很重要,可以判断你的服务是否有异常等,但源码包安装的日志是没有日志的轮询或者说是切割能力的, 所以你就需要用到bas ...
- WAMP启动失败简单解决方法
一般情况下,直接选择安装,突然出现问题了:提示:msvcp110.dll或msvcr110.dll问题, 那么你直接复制这个来百度就行. 在百度会提示让你一键安装并且修复的. 或者你可能会看网上其他教 ...
- webapp中fixed问题解决方案
主要问题: 1,头部输入框固定后,只要再滑动内容的话,输入框会随着滑动内容而滑动. 2,在低端机:2.3以下的安卓机,你会发现怎么解决都不行的,系统浏览器是不会支持的,头部底部固定的话会滑动内容而滑动 ...
- CSS 使用推荐
中文字体css编码转换 微软雅黑 \5FAE\8F6F\96C5\9ED1 或 Microsoft YaHei 黑体 \9ED1\4F53 新宋体 \65b0\5b8b\4f53 宋体 \5b8b ...
- go语言mongdb管道使用
原始json: { "listsn": "", "code": "fwq_add", "detail" ...
- xcode 插件
http://www.cocoachina.com/ios/20160122/15080.html https://github.com/rickytan/RTImageAssets http://m ...