博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Smallest rectangle Enclose Black Pixels
阅读量:7239 次
发布时间:2019-06-29

本文共 1807 字,大约阅读时间需要 6 分钟。

1 public class Solution { 2     private char[][] image; 3     public int minArea(char[][] image, int x, int y) { 4         if (image.length == 0 || image[0].length == 0) { 5             return 0; 6         } 7         this.image = image; 8         int n = image.length, m = image[0].length; 9         int left = searchRow(0, x, 0, m, true);10         int right = searchRow(x + 1, n, 0, m, false);11         int top = searchCols(0, y, 0, n, true);12         int bottom = searchCols(y + 1, m, 0, n, false);13         return (right - left) * (bottom - top);14     }15     16     private int searchCols(int i, int j, int top, int bottom, boolean bound) {17         while (i != j) {18             int k = top;19             int mid = (i + j) / 2;20             while (k < bottom && image[k][mid] == '0') {21                 k++;22             }23             if(k < bottom == bound) {24                 j = mid;25             } else {26                 i = mid + 1;27             }28         }29         return i;30     }   31     32     private int searchRow(int i, int j, int left, int right, boolean bound) {33         while (i != j) {34             int k = left;35             int mid = (i + j) / 2;36             while (k < right && image[mid][k] == '0') {37                 k++;38             }39             if(k < right == bound) {40                 j = mid;41             } else {42                 i = mid + 1;43             }44         }45         return i;46     }47 }

Binary search:

1. The bound is defined to search "0" or "1". When search 0 - x, we want to get the most left "1". So k < right mean there is a one, j = mid. But when we do x - n search, we want to get most left "0". So when k < right, we still need to keep searching mid + 1.

 

转载于:https://www.cnblogs.com/shuashuashua/p/5645609.html

你可能感兴趣的文章
nohup命令 使用方法详解
查看>>
Intent 和 Intent Filter
查看>>
Linux下MySQL的主从热备(自动同步)配置
查看>>
Windows 8 Consumer Preview版升级到 Release Preview 版后Metro应用(html5+JavaScript版)修改小结...
查看>>
编程珠玑:变位词程序的实现
查看>>
POJ 2987 Firing
查看>>
Newtonsoft.Json 应用
查看>>
HDU 1400 Mondriaan's Dream
查看>>
从零开始--系统深入学习android(实践-让我们开始写代码-Android框架学习-5.Android中的进程与线程)...
查看>>
如何在修改控件属性值,而不触发事件
查看>>
汉语转拼音之pinyin4j(转载)
查看>>
GNU make manual 翻译(六十)
查看>>
EffectiveC++ Item25说的东东
查看>>
stl学习总结简略
查看>>
Struts2中 No result defined for action com.test.action.LoginAction and result success
查看>>
对PostgreSQL源代码中的 ObjectClass的初步理解
查看>>
Oracle数据库系统性能优化策略
查看>>
rabbitmq使用__python客户端(消息发送者)
查看>>
bandwidth 0.32k 发布,内存带宽测试工具
查看>>
【笔记】谭浩强 《C程序设计》自学笔记系列01
查看>>