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 ...
随机推荐
- JS中检测数据类型的四种方式及每个方式的优缺点
//1.typeof 用来检测数据类型的运算符 //->typeof value //->返回值首先是一个字符串,其次里面包含了对应的数据类型,例如:"number". ...
- winform学习之----打开文件对话框并将文件内容放入文本框
OpenFileDialog ofg = new OpenFileDialog(); ofg.Title = "ddd";//设置对话框标题 ofg.Multiselect = t ...
- NSNumber的使用
1.NSNumber可以表示多种基本数据类型,如int.bool.char.float.double,以及他们加了修饰符long.unsigned的类型. 2.创建方法可以使用numberWi ...
- Scrum会议8(Beta版本)
组名:天天向上 组长:王森 组员:张政.张金生.林莉.胡丽娜 代码地址:HTTPS:https://git.coding.net/jx8zjs/llk.git SSH:git@git.coding.n ...
- Ajax解决IE浏览器兼容问题
ServletContext 被 Servlet 程序用来与 Web 容器通信.例如写日志,转发请求.每一个 Web 应用程序含有一个Context,被Web应用内的各个程序共享. 因为Context ...
- 【转】jsonp详解
原文地址:http://www.cnblogs.com/yuzhongwusan/archive/2012/12/11/2812849.html json相信大家都用的多,jsonp我就一直没有机会用 ...
- P4factory 运行结果展示 basic_routing 以及 ./run_all_tests 的运行结果
p4factory子目录下的run_all_tests 安装好p4factory之后,打算跑一下样例来测试是否正确. 但是,跑了targets目录内的basic_routing,make又报错了,但之 ...
- MySQL 命令行导出、导入Select 查询结果
<!-- 环境: Windows 2003 SP2 + MySQL5.5.28 Author: 博客园小dee --> 有的时候需要把在一张表中用 select 语句查询出来的结果保存到另 ...
- php的异步处理
在PHP Web程序中,发送手机短信.电子邮件.转换视频格式.记录日志.数据挖掘采集等,都是比较耗时的操作. 为了增强用户体验,需要将这些操作转为异步执行 PHP Web程序中的短耗时异步处理 前 ...
- 地图API使用文档-以腾讯地图为例
目录 腾讯地图API 2 1.API概览... 2 1.1 WebService API(官网注明是beta版本,可能不稳定,慎用):... 2 1.2 URL API:... 2 1.3 静态图AP ...