LeetCode 223 223. Rectangle Area

2020-02-25 16:23:56 浏览数 (1)

题目

计算矩形覆盖面积

代码语言:javascript复制
class Solution {
public:
    int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {
        
        int a =0;
        if(E<C&&G>A)
        {
            if(G>=C&&E>=A)
            {
               
                if(H>=D&&F<=B)
                {
                        a = (C-E)*(D-B);
                }
                else if(H>B&&H<D&&F<=B)
                    {
                        a = (C-E)*(H-B);
                    }
                    else if(H>=D&&F>B&&F<D)
                    {
                        a = (C-E)*(D-F);
                    }
                    else if(H<D&&F>B)
                    {
                        a = (C-E)*(H-F);
                    }
                
            }
            
            if(G<C&&E>=A)
            {
                
                if(H>=D&&F<=B)
                {
                        a = (G-E)*(D-B);
                }
                else if(H>B&&H<D&&F<=B)
                    {
                        a = (G-E)*(H-B);
                    }
                    else if(H>=D&&F>B&&F<D)
                    {
                        a = (G-E)*(D-F);
                    }
                    else if(H<D&&F>B)
                    {
                        a = (G-E)*(H-F);
                    }
            }
            
            if(E<A&&G<C)
            {
                 if(H>=D&&F<=B)
                {
                        a = (G-A)*(D-B);
                }
                else if(H>B&&H<D&&F<=B)
                    {
                        a = (G-A)*(H-B);
                    }
                    else if(H>=D&&F>B&&F<D)
                    {
                        a = (G-A)*(D-F);
                    }
                    else if(H<D&&F>B)
                    {
                        a = (G-A)*(H-F);
                    }
            }
            
            if(E<A&&G>=C)
            {
                 if(H>=D&&F<=B)
                {
                        a = (C-A)*(D-B);
                }
                else if(H>B&&H<D&&F<=B)
                    {
                        a = (C-A)*(H-B);
                    }
                    else if(H>=D&&F>B&&F<D)
                    {
                        a = (C-A)*(D-F);
                    }
                    else if(H<D&&F>B)
                    {
                        a = (C-A)*(H-F);
                    }
            }
        }
        
        return (C-A)*(D-B)-a (G-E)*(H-F);
        
    }
};

0 人点赞