题目链接这里
思路:求交集再减并集
public class Solution {
public int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {
if(A>E)
{
swap(A, E);
swap(B,F);
swap(C, G);
swap(D, H);
}
int overlatpt=0;
if(C<=E||B>=H||D<=F)
{
overlatpt=0;
}
else
{
int up=Math.min(D, H);
int down=Math.max(B, F);
int length=up-down;
int left=Math.max(A, E);
int right=Math.min(C, G);
int width=right-left;
overlatpt=length*width;
}
return (C-A)*(D-B)+(G-E)*(H-F)-overlatpt;
}
public static void swap(int A,int B)
{
int temp=A;
A=B;
B=temp;
return;
}
}