1 classSolution {2 Public:3 intComputearea (intAintBintCintDintEintFintGintH) {4 DoubleHeight_a=0, width_a=0, height_b=0, width_b=0, height_ol=0, width_ol=0;5 6 DoubleRightboundary =0;7 DoubleLeftboundary =0;8 DoubleUpperboundary =0;9 DoubleLowerboundary =0;Ten OneHeight_a = dB; AWidth_a = CA; -Height_b = hF; -Width_b = gE; the - if(c>g) Rightboundary =C; - ElseRightboundary =G; - + if(a<e) Leftboundary =A; - ElseLeftboundary =E; + A if(h>d) Upperboundary =H; at ElseUpperboundary =D; - - if(b<f) Lowerboundary =B; - ElseLowerboundary =F; - -Height_ol = height_a+height_b-(upperboundary-lowerboundary); inWidth_ol = width_a+width_b-(rightboundary-leftboundary); - to if(height_ol<0|| width_ol<0){ + returnHeight_a*width_a + height_b*Width_b; -}Else{ the returnHeight_a*width_a + height_b*width_b-height_ol*Width_ol; * } $ }Panax Notoginseng -};
The following speed will be much faster:
1 classSolution {2 Public:3 intComputearea (intAintBintCintDintEintFintGintH) {4 intoverlap=0;5 //Case 1:6 if(C>=e&&c<=g&&b>=f&&b<=h && E>=a&&e<=c&&h>=b &&h<=D) {7Overlap = (C-E) * (H-B);8 }9 //Case 2:Ten if(C>=e&&c<=g&&b>=f&&b<=h && c>=e&&c<=g&&d>=f &&d<=h && a<=d) { OneOverlap = (C-E) * (dB); A } - //Case 3: - if(C>=e&&c<=g&&d>=f&&d<=h && E>=a&&e<=c&&f>=b &&f<=D) { theOverlap = (C-E) * (dF); - } - //Case 4: - if(E>=a&&e<=c&&f>=b&&f<=d && E>=a&&e<=c&&h>=b &&h<=d && d>=h && b<=F) { +Overlap = (C-E) * (H-F); - } + //Case 5: A if(A>=e&&a<=g&&b>=f&&b<=h && c>=e&&c<=g&&b>=f &&b<=h && d>=H) { atOverlap = (c-a) * (H-B); - } - //Case 6 && Case-&& case: - if(A>=e&&a<=g&&b>=f&&b<=h && c>=e&&c<=g&&b>=f &&b<=h && a>=e&&a<=g&&d>=f&&d<=h && C>=E& &c<=g&&d>=f&&d<=i) { -Overlap = (c-a) * (dB); - } in //Case 7: - if(A>=e&&a<=g&&d>=f&&d<=h && c>=e&&c<=g&&d>=f &&d<=h && b<=F) { toOverlap = (c-a) * (dF); + } - //Case 8: the if(a>e&&c<=g && d>=h && b<=F) { *Overlap = (c-a) * (H-F); $ }Panax Notoginseng //Case 9: - if(A>=e&&a<=g&&b>=f&&b<=h && G>=a&&g<=c&&h>=b &&h<=D) { theOverlap = (g-a) * (H-B); + } A //Case Ten: the if(A>=e&&a<=g&&b>=f&&b<=h && a>=e&&a<=g&&d>=f &&d<=h && c>=G) { +Overlap = (g-a) * (dB); - } $ //Case One : $ if(A>=e&&a<=g&&d>=f&&d<=h && G>=a&&g<=c&&f>=b &&f<=e) { -Overlap = (g-a) * (dF); - } the //Case : - if(G>=a&&g<=c&&h>=b&&h<=d && G>=a&&g<=c&&f>=b &&f<=d && d>=h && b<=F) {WuyiOverlap = (g-a) * (H-F); the } - //Case : Wu if(E>=a&&g<=c && h>=d && f<=c) { -Overlap = (d-b) * (G-E); About } $ //Case : - if(E>=a&&e<=c&&f>=b&&f<=d && G>=a&&g<=c&&f>=b &&f<=d && E>=a&&e<=c&&h>=b&&h<=d && G>=A& &g<=c&&h>=b&&h<=D) { -Overlap = (G-E) * (H-F); - } A //Case : + if(E>=a&&e<=c&&f>=b&&f<=d && G>=a&&g<=c&&f>=b &&f<=d && h>=D) { theOverlap = (G-E) * (dF); - } $ //Case : the if(E>=a&&e<=c&&h>=b&&h<=d && G>=a&&g<=c&&h>=b &&h<=d && f<=c) { theOverlap = (G-E) * (H-B); the } the return((c-a) * (d-b) + (G-E) * (h-f)-overlap); - } in the};
leetcode:223. Rectangle Area