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 = 0y = 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的更多相关文章

  1. [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 ...

  2. LeetCode Smallest Rectangle Enclosing Black Pixels

    原题链接在这里:https://leetcode.com/problems/smallest-rectangle-enclosing-black-pixels/ 题目: An image is rep ...

  3. 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 ...

  4. [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 ...

  5. 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. ...

  6. [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 ...

  7. 【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 ...

  8. 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 ...

  9. LeetCode All in One 题目讲解汇总(持续更新中...)

    终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...

随机推荐

  1. phpcms使用细节

    1.在模板中使用php语句 <?php for ($i=0; $i < 10; $i++) {     echo $i."#######<br>"; }?& ...

  2. 我的J2EE学习历程

    由于最近手头没有JSP项目,所以暂停Hibernate和Spring的研究.个人觉得只有发现某个东西的不足之后再去学习新的东西来弥补这个不足比较好.就好比,最开始在JSP页面里面写Java代码,每次操 ...

  3. Java Runtime.availableProcessors()方法

    Java Runtime.availableProcessors()方法用法实例教程.   描述 java.lang.Runtime.availableProcessors() 方法返回到Java虚拟 ...

  4. ubuntu用apt-get安装memcache

    转自:http://yangfutao2000.blog.163.com/blog/static/12162588201151635856858/ 先安装服务器端: apt-get install m ...

  5. php 快速排序法

    function quicksort(array $arr = array()){ $len = count($arr); if ($len > 1) { $key = $arr[0]; $l_ ...

  6. STL删除元素

    1.从vector中删除多个元素: #include <iostream> #include <vector> int main() { std::vector<int& ...

  7. 响应性web设计实战总结

    响应性web设计实战 响应性web设计的理念是:页面的设计与开发应当根据用户行为与设备环境(包括系统平台,屏幕尺寸,屏幕定向等)进行相应的响应及调整.具体的实践方式由多方面组成,包括弹性网格和布局,图 ...

  8. Ionic 常见问题及解决方案

    前言 Ionic是目前较为流行的Hybird App解决方案,在Ionic开发过程中会遇到很多常见的开发问题,本文尝试对这些问题给出解决方案. 一些常识与技巧 list 有延迟,可以在ion-cont ...

  9. Book-编程珠玑-第一章

    第一章...二〇一六年十月二十五日 22:41:45 1MB存储空间里大约可以存143,000个号码; 如果每个号码都使用32位整数来表示的话,1MB存储空间里就可以存储250,000个号码; 看得迷 ...

  10. 必须知道的.net——学习笔记1

    1.对象的生成(出生) Person aperson=new Person("小张",25) 构造过程:分配存储空间—初始化附加成员—调用构造函数 2.对象的旅程(在一定的约定与规 ...