题目:
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.

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

翻译:
在二维平面中,计算被2个由直线围成的矩形所覆盖的总面积。
每个矩形都是由它左下角和右上角的点来定义的。

分析:
这道题目的关键是需要画图理清可能出现的情况。
1. 2个矩形不相交,这边分为2种情况,一个是第二个矩形在第一个矩形的上面或者下面;一个是第二个矩形在第一个矩形的左边或者右边;也可能2种都有,但是由于是或的关系,所以不用考虑。
2. 2个矩形相交,相交其实就是把4个x轴放在一起,找出其中大小在中间的2个,作为x轴;y轴同理。

代码:

public class Solution {public int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {int sum=(C-A)*(D-B)+(G-E)*(H-F);if(C<E||G<A||H<B||D<F){//没有交叉return sum;}else{//有交叉//计算重叠部分的x坐标,ACEGint x1=A<E?E:A;int x2=C<G?C:G;//计算重叠部分的y坐标, BDFHint y1=B<F?F:B;int y2=D<H?D:H;return sum-(x2-x1)*(y2-y1);}}
}

Leet Code OJ 223. Rectangle Area [Difficulty: Easy]相关推荐

  1. Leet Code OJ 112. Path Sum [Difficulty: Easy]

    题目: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up ...

  2. Leet Code OJ 344. Reverse String [Difficulty: Easy]

    题目: Write a function that takes a string as input and returns the string reversed. Example: Given s ...

  3. Leet Code OJ 28. Implement strStr() [Difficulty: Easy]

    题目: Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if ne ...

  4. Leet Code OJ 125. Valid Palindrome [Difficulty: Easy]

    题目: Given a string, determine if it is a palindrome, considering only alphanumeric characters and ig ...

  5. Leet Code OJ 20. Valid Parentheses [Difficulty: Easy]

    题目: Given a string containing just the characters , determine if the input string is valid. The brac ...

  6. Leet Code OJ 1. Two Sum [Difficulty: Easy]

    题目: Given an array of integers, return indices of the two numbers such that they add up to a specifi ...

  7. Leet Code OJ 189. Rotate Array [Difficulty: Easy]

    题目: Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the ar ...

  8. Leet Code OJ 66. Plus One [Difficulty: Easy]

    题目: Given a non-negative number represented as an array of digits, plus one to the number. The digit ...

  9. Leet Code OJ 27. Remove Element [Difficulty: Easy]

    题目: Given an array and a value, remove all instances of that value in place and return the new lengt ...

最新文章

  1. ansible-playbook组件解析及操作全解
  2. 24小时临时邮箱_实用网站 | 临时邮箱,悄悄来悄悄去~
  3. repository access denied. access via a deployment key is read-only.
  4. SpringCloud Gateway配置自定义路由404坑
  5. Java IO: 其他字节流(上)
  6. Delphi关于多线程同步的一些方法
  7. JVM(2)--一文读懂垃圾回收
  8. 使用BootStrap框架设置全局CSS样式
  9. 【LeetCode笔记】72. 编辑距离(Java、字符串、动态规划)
  10. 我们去的地方一定会有屎
  11. Java基础入门笔记-Getter与Setter
  12. php 非侵入式,Tideways和xhgui打造PHP非侵入式监控平台
  13. msvcrt python linux,Python msvcrt.CrtSetReportMode方法代码示例
  14. 腾讯内部转岗_别了,腾讯微博!
  15. [转载] Java Challengers#1:JVM中的方法重载
  16. 安装Baidu Sitemap Generator插件显示“该插件没有有效的标题”
  17. leafletjs 热力图_leaflet.js自定义热力图效果实例
  18. 漫射光层析成像的正问题理论模型(一)——蒙特卡洛模型概述
  19. 日照-公积金贷款逾期预测-比赛总结
  20. 基于Python的数字图形可视化水印系统的设计与实现

热门文章

  1. QT乱码总结8.编码测试和总结三
  2. 8.OD--函数参考
  3. 3_9 VisitorMode 访问者模式
  4. 浅谈 FTP、FTPS 与 SFTP
  5. 高级数据结构与算法 | 并查集(Union-Find)
  6. [Kafka与Spark集成系列一] Spark入门
  7. 深度阅读之《Concurrency in Go》
  8. Python中的网络编程之TCP
  9. 队列实现栈 | 栈实现队列
  10. DCT如此重要,作者当初竟然不知道?