Java: easy to understand


  • 0
    public class Solution {
        public int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {
            int begin = (C - A) * (D - B) + (G - E) * (H - F);
            return begin - overlap(A,B,C,D,E,F,G,H);
        }
        public int overlap(int A, int B, int C, int D, int E, int F, int G, int H) {
            if(C <= E || G <= A || B >= H || F >= D)
                return 0;
            int h = 0;
            if((D - H) * (B - F) >= 0)
                h = Math.min(Math.abs(H - B), Math.abs(D- F));
            else h = Math.min(Math.abs(D - B), Math.abs(H- F));
            int l = 0;
            if((A - E) * (C - G) >= 0)
                l = Math.min(Math.abs(A - G), Math.abs(E- C));
            else l = Math.min(Math.abs(A - C), Math.abs(E- G));
            return h * l;
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.