貌似現(xiàn)在leetcode多了rectangle是個(gè)點(diǎn)的check? 還有一個(gè)rectangle inside另外一個(gè)rectangle
感覺個(gè)人做 寫了很多extra if statement來check 不知道有沒有更好的解法
public int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {
// A to G's right or E to C's right to avoid X
// Similarly B(bottom!!!) >= H or F>= D
if ( (A >= G ) ||(E >= C) ||( B>=H ) ||(F >= D) ){
return (C-A)*(D-B) + (G-E)*(H-F);
}else{
//prevents points
if ((A==C)||(B==D)||(E==G)||(F==H)){
return (A-C)*(B-D) + (E-G)*(F-H) ;
}
// prevents inner within a larger rectangle
int maxLx = A - E > 0 ? A: E;
int maxLy = Math.max(B, F);
int minLx = Math.min(C,G);
int minLy = Math.min(D, H);
// check if inner rectangle
if ( ((E-A) >=0 && (C-G)>=0) && ((F-B)>=0) && ((D-H) >=0)||
( ((A-E)>=0)&& ((G-C)>=0 )&& ((B-F)>=0)&&((H-D)>=0) )
){
return (C-A)*(D-B) - (G-E)*(H-F) >=0 ? (C-A)*(D-B): (G-E)*(H-F);
}
int delta =(maxLx - minLx)*(maxLy - minLy);
return (C-A)*(D-B) + (G-E)*(H-F) - delta;
}
}
c8913be0da89 評(píng)論自Overlap Rectangle