https://leetcode.com/problems/rectangle-area/
今天做这道题发现了溢出的问题
1 int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {2 int overlap_width = max(min(C, G) - max(A, E), 0), overlap_height = max(min(D, H) - max(B, F), 0);3 return (C - A) * (D - B) + (G - E) * (H - F) - overlap_width * overlap_height;4 }
1 int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {2 int overlap_width = min(C, G) < max(A, E) ? 0 : min(C, G) - max(A, E);3 int overlap_height = min(D, H) < max(B, F) ? 0 : min(D, H) - max(B, F);4 return (C - A) * (D - B) + (G - E) * (H - F) - overlap_width * overlap_height;5 }
今天nvidia的电话面试也问了关于浮点数相加溢出的问题,关于并行相加将数进行分类的问题,之后详补。