1 class Solution: 2 def computeArea(self, A: int, B: int, C: int, D: int, E: int, F: int, G: int, H: int) -> int: 3 x = 0 4 y = 0 5 6 if (A <= E): 7 if (C <= E): 8 x = 0 9 elif (C >= G): 10 x = G - E 11 else: 12 x = C - E 13 elif (G <= A): 14 x = 0 15 elif (G >= C): 16 x = C - A 17 else: 18 x = G - A 19 20 if (B <= F): 21 if (D <= F): 22 y = 0 23 elif (D >= H): 24 y = H - F 25 else: 26 y = D - F 27 elif (H <= B): 28 y = 0 29 elif (H >= D): 30 y = D - B 31 else: 32 y = H - B 33 34 return (C - A) * (D - B) + (G - E) * (H - F) - x * y
算法思路,各种条件判断,画一幅图表示吧,对角的图形有重复的的,因此四个对角一共4种,其他位置一共10种,总共是14种情况。