火柴拼出多少个正方形 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的更多相关文章

  1. 【CS Round #44 (Div. 2 only) D】Count Squares

    [链接]点击打开链接 [题意] 给你一个0..n和0..m的区域. 你可以选定其中的4个点,然后组成一个正方形. 问你可以圈出多少个正方形. (正方形的边不一定和坐标轴平行) [题解] 首先,考虑只和 ...

  2. C Primer Plus(第五版)5

    第5章 运算符,表达式和语句 5.1 循环简单 程序清单 5.1 显示了一个示例程序,该程序做了一点算术运算来计算穿 9 码鞋的脚用英寸表示的长度.为了增加你对循环的理解,程序的第一版演示了不使用循环 ...

  3. nodejs api 中文文档

    文档首页 英文版文档 本作品采用知识共享署名-非商业性使用 3.0 未本地化版本许可协议进行许可. Node.js v0.10.18 手册 & 文档 索引 | 在单一页面中浏览 | JSON格 ...

  4. LeetCode Top Interview Questions

    LeetCode Top Interview Questions https://leetcode.com/problemset/top-interview-questions/ # No. Titl ...

  5. [LeetCode] Word Squares 单词平方

    Given a set of words (without duplicates), find all word squares you can build from them. A sequence ...

  6. [LeetCode] Count Primes 质数的个数

    Description: Count the number of prime numbers less than a non-negative number, n click to show more ...

  7. [LintCode] Count and Say 计数和读法

    The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, 1211, 111221 ...

  8. HDU 1264 Counting Squares(线段树求面积的并)

    Counting Squares Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  9. [leetcode] Count Primes

    Count Primes Description: Count the number of prime numbers less than a non-negative number, n click ...

随机推荐

  1. (function(){})()的用法

    最近在整理javascript 学习,发现这个问题了 ,在网上发现这么个解释 最清楚 最明白 : (function(){})() 相当于先定义 function xx(){},后调用 xx(); ( ...

  2. 关于sql语句的优化问题

    系统要求进行SQL优化,对效率比较低的SQL进行优化,使其运行效率更高,其中要求对SQL中的部分in/not in修改为exists/not exists 修改方法如下: in的SQL语句 SELEC ...

  3. python中迭代器和生成器

    l=[1,2,3,4] for n in l: print n 在看上面这段代码的时候,我们没有显式的控制列表的偏移量,就可以自动的遍历了整个列表对象.那么for 语句是怎么来遍历列表l的呢?要回答这 ...

  4. c#String的不变特性,可读但不可写性

    谈到字符串,大家自然觉得简单,但是总是有一些小的问题隐约出现,下面我就系统的说一下字符串的问题,有说不到日后再予补充. 1,首先String是一个类,string只是String类的一个别名,别名的意 ...

  5. UAPStudio授权过期的解决方法,重新授权

    1.启动lisence服务器,生成硬件锁, 并导入授权. 需要注意的地方:1.点击工具栏“帮助”下的“UAP-STUDIO”授权管理. 2.删除“D:\UAP-STUDIO\Platform\bin” ...

  6. ERROR com.opensymphony.xwork2.interceptor.ParametersInterceptor.error:34 - Developer Notification

    We are trying to migrate from Struts 2.2 to Struts 2.3, after getting to run the project I am seeing ...

  7. Google Chrome can not be run as root

    Ubuntu运行Chrome出现"Google Chrome can not be run as root"的解决方法 编辑启动文件:/opt/google/chrome/goog ...

  8. PHP报错: Can't use method return value in write context

    $dp_id = $this->getParam('dpId'); if(!empty($this->getParam('dpId'))) { $this->smarty->a ...

  9. communicate with other processes, regardless of where they are running

    Advanced Programming in the UNIX Environment Third Edition   In the previous chapter, we looked at p ...

  10. MySQL问题汇总(持续更新)

    1.This function has none of DETERMINISTIC, NO SQL 原因: Mysql如果开启了bin-log, 我们就必须指定我们的函数是否是 1 DETERMINI ...