Interview How to Count Squares
火柴拼出多少个正方形 http://matchstickpuzzles.blogspot.com/2011/06/55-4x4-square-how-many-squares.html
输入是两个二维数组ver 和 hor, 若是有火柴就是1, 没有就是0.
dpHor 表示横方向上有多少连续火柴,dpVer表示纵方向上有多少连续火柴。
最后以左上角第一根横着的火柴为根基检查是否能组成正方形。
Time Complexity: O(n^3). Space: O(n^2).
import java.util.*;
public class countSquare{
public static void main(String [] args){
int [][] hor = {{1,1},{1,0},{1,1}};
int [][] ver = {{1,1,1},{1,1,1}};
System.out.println("Number of square: " + countSquare(hor,ver));
} private static int countSquare(int [][] hor, int [][] ver){
if(hor == null || ver == null || hor.length == 0 || ver.length == 0 || hor[0].length == 0 || ver[0].length == 0){
return 0;
} int [][] dpHor = new int[hor.length][hor[0].length];
int [][] dpVer = new int[ver.length][ver[0].length]; for(int i = 0; i<hor.length; i++){
for(int j = 0; j<hor[0].length; j++){
if(hor[i][j] == 1){
dpHor[i][j] = j == 0 ? 1 : dpHor[i][j-1] + 1;
}else{
dpHor[i][j] = 0;
}
}
} for(int j = 0; j<ver[0].length; j++){
for(int i = 0; i<ver.length; i++){
if(ver[i][j] == 1){
dpVer[i][j] = i == 0 ? 1 : dpVer[i-1][j] + 1;
}else{
dpVer[i][j] = 0;
}
}
} System.out.println("dpHor is " + Arrays.deepToString(dpHor));
System.out.println("dpVer is " + Arrays.deepToString(dpVer)); int res = 0;
for(int i = 0; i<hor.length; i++){
for(int j = 0; j<hor[0].length; j++){
for(int len = 1; len<= Math.min(ver.length-i, hor[0].length-j); len++){
if(dpHor[i][j+len-1] >= len && dpHor[i+len][j+len-1] >= len && dpVer[i+len-1][j] >= len && dpVer[i+len-1][j+len] >= len){
res++;
System.out.println("i = " + i + ", j = " + j + ", len = " + len + ", res = " + res);
}
}
}
}
return res;
}
}
Interview How to Count Squares的更多相关文章
- 【CS Round #44 (Div. 2 only) D】Count Squares
[链接]点击打开链接 [题意] 给你一个0..n和0..m的区域. 你可以选定其中的4个点,然后组成一个正方形. 问你可以圈出多少个正方形. (正方形的边不一定和坐标轴平行) [题解] 首先,考虑只和 ...
- C Primer Plus(第五版)5
第5章 运算符,表达式和语句 5.1 循环简单 程序清单 5.1 显示了一个示例程序,该程序做了一点算术运算来计算穿 9 码鞋的脚用英寸表示的长度.为了增加你对循环的理解,程序的第一版演示了不使用循环 ...
- nodejs api 中文文档
文档首页 英文版文档 本作品采用知识共享署名-非商业性使用 3.0 未本地化版本许可协议进行许可. Node.js v0.10.18 手册 & 文档 索引 | 在单一页面中浏览 | JSON格 ...
- LeetCode Top Interview Questions
LeetCode Top Interview Questions https://leetcode.com/problemset/top-interview-questions/ # No. Titl ...
- [LeetCode] Word Squares 单词平方
Given a set of words (without duplicates), find all word squares you can build from them. A sequence ...
- [LeetCode] Count Primes 质数的个数
Description: Count the number of prime numbers less than a non-negative number, n click to show more ...
- [LintCode] Count and Say 计数和读法
The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, 1211, 111221 ...
- HDU 1264 Counting Squares(线段树求面积的并)
Counting Squares Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- [leetcode] Count Primes
Count Primes Description: Count the number of prime numbers less than a non-negative number, n click ...
随机推荐
- 【C语言】07-基本语句和运算
一.基本语句 C语言的基本语句跟Java中的差不多,所以,这里只是简单地提一下 循环语句(do while.while.for) 条件语句(if .if-else.switch) goto语句 二.基 ...
- 微课程--Android--基础控件的使用
view viewgroup是一种特殊的view,里面可以包含其他的view 如何生成view: 1 在代码里动态生成 2 写在XML里面 view的常见属性--宽度 wrap_content 随着内 ...
- mysql多种方法修改密码----5.6的坑
创建用户并授权和改密码: grant all privileges on *.* to root@'%' identified by '123456' with grant option; * ...
- 免杀ASP一句话
<% wei="日日日)""wei""(tseuqer lave 日" execute(UnEncode(wei)) function ...
- Hadoop配置文件解析
Hadoop源码解析 2 --- Hadoop配置文件解析 1 Hadoop Configuration简介 Hadoop没有使用java.util.Properties管理配置文件, 也没有使 ...
- fastjson生成json时Null属性不显示
举个例子 生成JSON代码片段 Map < String , Object > jsonMap = new HashMap< String , Object>(); jsonM ...
- Nginx 配置 Basic 认证
/* * 环境:LNMP(CentOS 6.6 + Nginx 1.8.0) */ 在 Nginx 下配置 Basic 认证需要依靠 Nginx 的 http_auth_basic_module 模块 ...
- TCP协议中的三次握手和四次挥手(图解)(转载http://blog.csdn.net/whuslei/article/details/6667471)
建立TCP需要三次握手才能建立,而断开连接则需要四次握手.整个过程如下图所示: 先来看看如何建立连接的 首先Client端发送连接请求报文,Server段接受连接后回复ACK报文,并为这次连接分配资源 ...
- Bootstrap页面布局13 - BS按钮
bootstrap中的按钮类 一般可以作为按钮的标签有:<a></a> <button></button> <input type='butt ...
- 21335592 ROWS
CREATE TABLE w_big SELECT * FROM ( SEELCT * FROM w_tab UNION ALL SELECT * FROM w_tab_copy_modify ) ...