• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
neverlandly
博客园    首页    新随笔    联系   管理    订阅  订阅

Leetcode: Rectangle Area

Find the total area covered by two rectilinear rectangles in a 2D plane.

Each rectangle is defined by its bottom left corner and top right corner as shown in the figure.

Rectangle Area
Assume that the total area is never beyond the maximum possible value of int.

Rectangle Area

 1 public class Solution {
 2     public int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {
 3         int area3 = 0;
 4         int area1 = (C-A) * (D-B);
 5         int area2 = (G-E) * (H-F);
 6         if (C<=E || G<=A || H<=B || D<=F) area3 = 0;
 7         else area3 = (Math.min(C,G)-Math.max(A,E)) * (Math.min(D,H)-Math.max(B,F));
 8         return area1 + area2 - area3;
 9     }
10 }

 

posted @ 2015-12-18 00:48  neverlandly  阅读(324)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3