LeetCode 391: Perfect Square
Posted keepshuatishuati
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LeetCode 391: Perfect Square相关的知识,希望对你有一定的参考价值。
Note:
All the points should be even since it will be dispeared by perfect joint. Thus only 4 points will be left.
class Solution { public boolean isRectangleCover(int[][] rectangles) { int lx = Integer.MAX_VALUE, ly = Integer.MAX_VALUE, rx = 0, ry = 0, sum = 0; HashSet<String> set = new HashSet<String>(); for (int[] rec : rectangles) { lx = Math.min(rec[0], lx); ly = Math.min(rec[1], ly); rx = Math.max(rec[2], rx); ry = Math.max(rec[3], ry); sum += (rec[2] - rec[0]) * (rec[3] - rec[1]); String s1 = rec[0] + " " + rec[1]; String s2 = rec[0] + " " + rec[3]; String s3 = rec[2] + " " + rec[3]; String s4 = rec[2] + " " + rec[1]; if (!set.add(s1)) set.remove(s1); if (!set.add(s2)) set.remove(s2); if (!set.add(s3)) set.remove(s3); if (!set.add(s4)) set.remove(s4); } if (!set.contains(lx + " " + ly) || !set.contains(lx + " " + ry) || !set.contains(rx + " " + ly) || !set.contains(rx + " " + ry) || set.size() != 4) return false; return sum == ((rx - lx) * (ry - ly)); } }
以上是关于LeetCode 391: Perfect Square的主要内容,如果未能解决你的问题,请参考以下文章
java 391. Perfect Rectangle.java