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.

计算图形面积;

两个矩形面积减去重复的面积即可。

public class Solution {
    public int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {

        int area = (C - A) * (D - B) + (G - E) * (H - F);

        if (B > H || F > D || A > G || C < E)
            return area;
        int x1 = Math.max(A, E);
        int y1 = Math.max(B, F);
        int x2 = Math.min(C, G);
        int y2 = Math.min(D, H);
        int result = (x2 - x1) * (y2 - y1);
        if (result > 0){
            return area - result;
        } else {
            return area + result;
        }
    }
}
时间: 2024-10-10 12:04:53

leetcode 223. Rectangle Area 计算面积---------- java的相关文章

[LeetCode]223. Rectangle Area矩形面积

/* 像是一道数据分析题 思路就是两个矩形面积之和减去叠加面积之和 */ public int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) { //求两个面积 int a1 = (C-A)*(D-B); int a2 = (G-E)*(H-F); //求叠加面积,(低上限-高下限)*(左右线-右左线) int h1 = Math.min(D,H); int h2 = Math.max(B,F); 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 223 Rectangle Area(矩形面积)

翻译 找到在二维平面中两个相交矩形的总面积. 每一个矩形都定义了其左下角和右上角的坐标. (矩形例如以下图) 如果,总占地面积永远不会超过int的最大值. 原文 分析 这题前天试过,写了一堆推断.终究还是无果-- 贴几个别人的解决方式-- int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) { int64_t xmin1 = min( A, C ); int64_t xmax1 = max( A, C )

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

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

No.223 Rectangle Area

No.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 pos

[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 OJ 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. 对于

【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