Rectangle Area

来说说看问题急躁,一开始就理所当然的想问的是overlapped area,实际上是total。

脑子里一堆边界条件,看了ref觉得很简洁 http://blog.csdn.net/foreverling/article/details/46411069

 

public class Solution {

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

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

        if(C<=E||D<=F||A>=G||B>=H)

            return area;

        int top = Math.min(D,H);

        int bt = Math.max(B,F);

        int left = Math.max(A,E);

        int right = Math.min(C,G);

        return area - (top-bt)*(right-left);

    }

}

 

你可能感兴趣的:(c)