leetcode 223: Rectangle Area

There are just two conditions that two rectangles can intersect or not. Draw out all possibilities and you can find out the solution.

class Solution {
public:
    int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {
        int size1=(D-B)*(C-A);
        int size2=(H-F)*(G-E);
        if(E>=C||F>=D||A>=G||B>=H)
            return size1+size2;
        return size1+size2-(min(D,H)-max(B,F))*(min(C,G)-max(A,E));
    }
};


發佈了186 篇原創文章 · 獲贊 0 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章