leetCode(33):Rectangle Area

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 in the figure.

Assume that the total area is never beyond the maximum possible value of int.

class Solution {
public:
    int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {
        int area1=(C-A)*(D-B);
    	int area2=(G-E)*(H-F);
    	if(B>=H || F>=D || E>=C || G<=A)
    	{//如果不相交
    		return area1+area2;
    	}
    	else
    	{
    		int length,width;
    		width=min(C,G)-max(A,E);//宽
    		length=min(D,H)-max(B,F);//长
    		return area1+area2-length*width;//面积之和减去重合部分面积
    	}
    }
};

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-21 00:28:12

leetCode(33):Rectangle Area的相关文章

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] 850. Rectangle Area II 矩形面积之二

We are given a list of (axis-aligned)?rectangles.? Each?rectangle[i] = [x1, y1, x2, y2]?, where (x1, y1) are the coordinates of the bottom-left corner, and (x2, y2) are the coordinates of the top-right corner of the?ith rectangle. Find the total area

leetcode之Rectangle Area

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 in the figure. Assume that the total area is never beyond the maximum possible value of int. 这道

【Leetcode】Rectangle Area

题目链接:https://leetcode.com/problems/rectangle-area/ 题目: 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 in the figure. Assume that the total are

leetcode 223. Rectangle Area 计算面积---------- java

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 in the figure. Assume that the total area is never beyond the maximum possible value of int. 计算

Java for LeetCode 223 Rectangle Area

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 in the figure. Assume that the total area is never beyond the maximum possible value of int. Cr

leetcode:Rectangle Area

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 in the figure. Assume that the total area is never beyond the maximum possible value of int. 本题

LeetCode 850. Rectangle Area II

经典line sweep问题,和 perfect rectangle 很类似,但是要考虑多个矩形左边界一样的情况,加个id来区分. 和另一道类似的题 the skyline problem 不同的是,没有很多 corner cases,对 x 排序非常简单. 难点是在 active set 里,和 merge intervals 那题一样,计算 total_y,乘以两个相邻 event 的 delta_x. class Solution { public: struct interval { i

Leetcode 223 Rectangle Area

求相交矩形的面积 1 class Solution { 2 public: 3 int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) { 4 int dx = max(A,E); 5 int dy = max(B,F); 6 int ux = min(C,G); 7 int uy = min(D,H); 8 if(ux>dx&&uy>dy){ 9 return (C-A)*(D-B)+(G