LeetCode-Rectangle Area

好多字母 有点晕 

本来想使用总的大矩形减去两个小的 结果假如一个包含另一个就不适用

于是用计算overlap面积 判断是否overlap的条件很重要

public class Solution {
    public int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {
        int left = Math.max(A,E);
        int right = Math.min(C,G);
        int bottom = Math.max(B,F);
        int top = Math.min(D,H);
        int overlap = 0;
        if ( right > left && top > bottom)
            overlap = (right-left) * ( top - bottom);
        return (C-A)*(D-B) + (G-E) * (H-F) - overlap;
    }
}


你可能感兴趣的:(LeetCode-Rectangle Area)