美文网首页Leetcode
223. Rectangle Area

223. Rectangle Area

作者: oo上海 | 来源:发表于2016-08-08 00:30 被阅读19次

    223. Rectangle Area

    题目:
    https://leetcode.com/problems/add-two-numbers/

    难度 : Easy

    这道题是我瞎了狗眼,🐶,之前看错了,以为要求相交的部分,结果是求cover的部分,所以写的长||||||

    class Solution(object):
        def computeArea(self, A, B, C, D, E, F, G, H):
            """
            :type A: int
            :type B: int
            :type C: int
            :type D: int
            :type E: int
            :type F: int
            :type G: int
            :type H: int
            :rtype: int
            """
            return self.area(C - A, D - B) + self.area(H - F, G - E ) - self.area(self.interSect(A,C,E,G), self.interSect(B,D,F,H))
                
        def area(self, w, h):
            if w * h < 0:
                return - w * h
            return w * h
            
        
        def interSect(self, A, C, E, G):
            if E > C:
                return 0
            elif G < A:
                return 0
            elif E >= A and G <= C:
                return G - E
            elif A >= E and C <= G:
                return C - A
            elif G <= C and G >= A and E <= A:
                return G - A
            else:
                return C - E
                
                
    

    相关文章

      网友评论

        本文标题:223. Rectangle Area

        本文链接:https://www.haomeiwen.com/subject/qdolsttx.html