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上有很多所谓“一句话就实现”的代码。不过我觉得那些代码的可读性太差。下面是我写的代码:

#include<stdio.h>
// 计算两条线段重合的部分,如果没有重合的部分,则输出0// 以X轴为示例,先找到两条线段左侧坐标的最大值// 再找到两条线段右侧坐标的最小值// 然后用右侧坐标的最小值减去左侧坐标的最大值// 如果相减得到的值小于0,则表示没有重合// 如果相减得到的值大于等于0,则表示有重合// 相减得到的值即为重合部分的长度
int coverLength(int A, int C, int E, int G){
    int maxA = 0;
    if (A > E){
        maxA = A;
    }
    else{
        maxA = E;
    }

    int minC = 0;
    if (C < G){
        minC = C;
    }
    else{
        minC = G;
    }

    int outInt = minC - maxA;
    if (outInt < 0){
        return 0;
    }
    return outInt;
}

int computeRetangleArea(int A, int B, int C, int D){
    return (C - A)* (D - B);
}
// 计算出两个矩形的面积之和,然后再减去重合部分的面积
int computeArea(int A, int B, int C, int D, int E, int F, int G, int H){
    int x = coverLength(A, C, E, G);
    int y = coverLength(B, D, F, H);
    int coverArea = x*y;
    int firstArea = computeRetangleArea(A, B, C, D);
    int lastArea = computeRetangleArea(E, F, G, H);
    return firstArea + lastArea - coverArea;
}

void main(){
    int area = computeArea(0, 0, 0, 0, -1, -1, 1, 1);
    printf("%d\n", area);
}
时间: 2024-10-08 16:37:01

leetcode之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

题目链接: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(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. cl

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