日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

Leetcode题目:Rectangle Area

發(fā)布時間:2025/5/22 48 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Leetcode题目:Rectangle Area 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

題目:

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) {
??????? if( (A > C) || (B > D) || (E > G) || (F > H) )
??????? {
??????????? return 0;
??????? }
??????? int a = (C - A) * (D - B);
??????? int b = (G - E) * (H - F);
???????
??????? int intersection = 0;
??????? int A_ = max(A,E);
??????? int B_ = max(B,F);
??????? int C_ = min(C,G);
??????? int D_ = min(D,H);
??????? if((A_ < C_) && (B_ < D_))
??????? {
??????????? intersection = (C_ - A_) * (D_ - B_);
??????? }
??????? return a + b - intersection;
??? }
};

轉(zhuǎn)載于:https://www.cnblogs.com/CodingGirl121/p/5442598.html

總結(jié)

以上是生活随笔為你收集整理的Leetcode题目:Rectangle Area的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。