Leetcode 223 Rectangle Area, leetcoderectangle
1. Problem Description
Calculate the area of the two rectangles.
2. methods and ideas
You can first obtain the intersection area of the two rectangles, and then use the sum of the two rectangles minus the intersection area to represent the rectangle and area.
Note: although the area does not exceed the maximum value of int, the length of the middle side may exceed. Pay attention to the handling details; otherwise, overflow may occur.
class Solution {public: int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) { int unionArea =0; long wlimit,hlimit; wlimit = (long)min(C,G)-(long)max(A,E); hlimit = min(D,H)-max(B,F); if(wlimit >=0 && hlimit >= 0) { unionArea = (min(C,G)-max(A,E))* (min(D,H)-max(B,F)); } else { unionArea = 0; } //printf("test:%lu\n",wlimit); return (C - A)*(D - B) + (G - E)*(H - F) - unionArea; }};int main(){ Solution sol; printf("%d\n",sol.computeArea(-3,0,3,4,0,-1,9,2)); int a[8] = {-1500000001,0,-1500000000,1,1500000000,0,1500000001,1}; //for(int i = 0; i < 8; i ++) scanf("%d",&a[i]); printf("%d\n",sol.computeArea(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7])); return 0;}