240 發(fā)簡(jiǎn)信
IP屬地:安大略
  • 貌似現(xiàn)在leetcode多了rectangle是個(gè)點(diǎn)的check? 還有一個(gè)rectangle inside另外一個(gè)rectangle
    感覺個(gè)人做 寫了很多extra if statement來check 不知道有沒有更好的解法

    public int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {
    // A to G's right or E to C's right to avoid X
    // Similarly B(bottom!!!) >= H or F>= D
    if ( (A >= G ) ||(E >= C) ||( B>=H ) ||(F >= D) ){
    return (C-A)*(D-B) + (G-E)*(H-F);
    }else{
    //prevents points
    if ((A==C)||(B==D)||(E==G)||(F==H)){
    return (A-C)*(B-D) + (E-G)*(F-H) ;
    }
    // prevents inner within a larger rectangle

    int maxLx = A - E > 0 ? A: E;
    int maxLy = Math.max(B, F);
    int minLx = Math.min(C,G);
    int minLy = Math.min(D, H);

    // check if inner rectangle
    if ( ((E-A) >=0 && (C-G)>=0) && ((F-B)>=0) && ((D-H) >=0)||
    ( ((A-E)>=0)&& ((G-C)>=0 )&& ((B-F)>=0)&&((H-D)>=0) )
    ){

    return (C-A)*(D-B) - (G-E)*(H-F) >=0 ? (C-A)*(D-B): (G-E)*(H-F);
    }
    int delta =(maxLx - minLx)*(maxLy - minLy);

    return (C-A)*(D-B) + (G-E)*(H-F) - delta;
    }

    }

  • 貌似現(xiàn)在leetcode多了rectangle是個(gè)點(diǎn)的check嗜暴? 還有一個(gè)rectangle inside另外一個(gè)rectangle
    感覺個(gè)人做 寫了很多extra if statement來check 不知道有沒有更好的解法

    public int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {
    // A to G's right or E to C's right to avoid X
    // Similarly B(bottom!!!) >= H or F>= D
    if ( (A >= G ) ||(E >= C) ||( B>=H ) ||(F >= D) ){
    return (C-A)*(D-B) + (G-E)*(H-F);
    }else{
    //prevents points
    if ((A==C)||(B==D)||(E==G)||(F==H)){
    return (A-C)*(B-D) + (E-G)*(F-H) ;
    }
    // prevents inner within a larger rectangle

    int maxLx = A - E > 0 ? A: E;
    int maxLy = Math.max(B, F);
    int minLx = Math.min(C,G);
    int minLy = Math.min(D, H);

    // check if inner rectangle
    if ( ((E-A) >=0 && (C-G)>=0) && ((F-B)>=0) && ((D-H) >=0)||
    ( ((A-E)>=0)&& ((G-C)>=0 )&& ((B-F)>=0)&&((H-D)>=0) )
    ){

    return (C-A)*(D-B) - (G-E)*(H-F) >=0 ? (C-A)*(D-B): (G-E)*(H-F);
    }
    int delta =(maxLx - minLx)*(maxLy - minLy);

    return (C-A)*(D-B) + (G-E)*(H-F) - delta;
    }

    }

    Overlap Rectangle

    這道題與Leetcode 223題RectangleArea十分相似凸克。 題目 給定兩個(gè)長(zhǎng)方形左下角和右上角的坐標(biāo),判斷是否有重疊闷沥,返回true或者false。 Java 代碼...

亚洲A日韩AV无卡,小受高潮白浆痉挛av免费观看,成人AV无码久久久久不卡网站,国产AV日韩精品