391 Perfect Rectangle 完美矩形

有 N 个与坐标轴对齐的矩形, 其中 N > 0, 判断它们是否能精确地覆盖一个矩形区域。
每个矩形用左下角的点和右上角的点的坐标来表示。例如, 一个单位正方形可以表示为 [1,1,2,2]。 ( 左下角的点的坐标为 (1, 1) 以及右上角的点的坐标为 (2, 2) )。

详见:https://leetcode.com/problems/perfect-rectangle/description/

C++:

    class Solution {
    public:
        bool isRectangleCover(vector<vector<int>>& rectangles) {
            unordered_map<string, int> hash;
            for(auto val: rectangles)
            {
                for(int i = 0; i < 4; i++)
                {
                    string tem = to_string(val[i/2*2])+‘,‘+to_string(val[i%2*2+1]);
                    if(hash[tem]&(1<<i))
                    {
                        return false;
                    }
                    hash[tem] |= (1<<i);
                }
            }
            int cntCorner = 0;
            for(auto& val: hash)
            {
                int sec = val.second;
                if(!(sec&(sec-1)) && cntCorner++ > 4)
                {
                    return false;
                }
                if((sec&(sec-1)) && !(sec==3||sec==12||sec==5||sec==10||sec==15))
                {
                    return false;
                }
            }
            return true;
        }
    };

参考:https://blog.csdn.net/qq508618087/article/details/52483625

原文地址:https://www.cnblogs.com/xidian2014/p/8849582.html

时间: 2024-10-16 12:59:18

391 Perfect Rectangle 完美矩形的相关文章

Perfect Rectangle(完美矩形)

我们有 N 个与坐标轴对齐的矩形, 其中 N > 0, 判断它们是否能精确地覆盖一个矩形区域. 每个矩形用左下角的点和右上角的点的坐标来表示.例如, 一个单位正方形可以表示为 [1,1,2,2]. ( 左下角的点的坐标为 (1, 1) 以及右上角的点的坐标为 (2, 2) ). 示例 1: rectangles = [ [1,1,3,3], [3,1,4,2], [3,2,4,4], [1,3,2,4], [2,3,3,4] ] 返回 true.5个矩形一起可以精确地覆盖一个矩形区域. 示例 2

391. Perfect Rectangle

Given N axis-aligned rectangles where N > 0, determine if they all together form an exact cover of a rectangular region. Each rectangle is represented as a bottom-left point and a top-right point. For example, a unit square is represented as [1,1,2,2

[Swift]LeetCode391. 完美矩形 | Perfect Rectangle

Given N axis-aligned rectangles where N > 0, determine if they all together form an exact cover of a rectangular region. Each rectangle is represented as a bottom-left point and a top-right point. For example, a unit square is represented as [1,1,2,2

LeetCode:Rectangle Area - 矩形交叉部分的面积

1.题目名称 Rectangle Area(矩形交叉部分的面积) 2.题目地址 https://leetcode.com/problems/rectangle-area/ 3.题目内容 英文:Find the total area covered by two rectilinear rectangles in a 2D plane. Each rectangle is defined by its bottom left corner and top right corner as shown

[LeetCode] Perfect Number 完美数字

We define the Perfect Number is a positive integer that is equal to the sum of all its positive divisors except itself. Now, given an integer n, write a function that returns true when it is a perfect number and false when it is not. Example: Input:

[LeetCode] Maximal Rectangle 最大矩形

Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and return its area. 此题是之前那道的Largest Rectangle in Histogram 直方图中最大的矩形 的扩展,这道题的二维矩阵每一层向上都可以看做一个直方图,输入矩阵有多少行,就可以形成多少个直方图,对每个直方图都调用Largest Rectangle in Hist

Leetcode: Perfect Rectangle

Given N axis-aligned rectangles where N > 0, determine if they all together form an exact cover of a rectangular region. Each rectangle is represented as a bottom-left point and a top-right point. For example, a unit square is represented as [1,1,2,2

设计并测试一个名为Rectangle的矩形类

//其属性为矩形的左下角和右上角两个点的坐标,能计算矩形的面积. #include <iostream> using namespace std; class Rectangle { public : Rectangle() {}; Rectangle(int iLeftX,int iLeftY,int iRightX,int iRightY) { this->iLeftX=iLeftX; this->iLeftY=iLeftY; this->iRightX=iRightX;

[LeetCode] Rectangle Area 矩形面积

Find the total area covered by two rectilinear rectangles in a2D plane. Each rectangle is defined by its bottom left corner and top right corner as shown in the figure. Assume that the total area is never beyond the maximum possible value of int. Cre