Java 5 line solution,easy to understand


  • 5
    public int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {
    	int area1 = Math.abs((D-B)*(C-A));
    	int area2 = Math.abs((G-E)*(H-F));
    	int repeat_h = Math.max(B,F)>Math.min(D,H) ? 0 : Math.min(D,H)-Math.max(B, F);
    	int repeat_w = Math.max(A,E)>Math.min(C,G) ? 0 : Math.min(C,G)-Math.max(A, E);
        return area1+area2-(repeat_h*repeat_w);
    }

  • 0
    C

    The first two lines can be simplified:
    int res = (C-A)(D-B) + (G-E)(H-F);
    Abs is not necessary, cause your algorithm including the way to calculate repeated part is valid if only the triangle is given correctly


  • 0
    Y

    This is not easy to understand at all LOL


Log in to reply
 

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