【LEETCODE】223-Rectangle Area
2015-12-28 12:23
274 查看
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.
题意:
找到两个 二维平面上的直线 矩形的并集的面积
每个矩形由左下角和右上角的坐标决定
假设整体面积不会超过int的最大值
思路:
求出两个矩形的面积,再减去交集的面积,注意不相交时,直接两个面积相加
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.
题意:
找到两个 二维平面上的直线 矩形的并集的面积
每个矩形由左下角和右上角的坐标决定
假设整体面积不会超过int的最大值
思路:
求出两个矩形的面积,再减去交集的面积,注意不相交时,直接两个面积相加
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 """ la=C-A wa=D-B lb=G-E wb=H-F sa=la*wa sb=lb*wb if C<=E or G<=A or D<=F or H<=B: #这些就是不相交的情况 return sa+sb else: lset=sorted([A,C,E,G]) #直接用sort,没有考虑到不相交的情况 lab=lset[2]-lset[1] wset=sorted([B,D,F,H]) wab=wset[2]-wset[1] sab=lab*wab return sa+sb-sab
相关文章推荐
- Quartz2D-绘线
- Bezier曲线
- context
- YII使用url组件美化管理的方法
- python 遍历文件夹和子目录抓出自己想用的文件类型
- 2015年12月28日课堂笔记与作业
- phpMyAdmin 手动输入数据库服务器IP
- ios-@property属性(特性)
- window.load和document.ready的区别
- 3D touch功能
- 恩布企业 IM 安卓端 1.3,服务端 1.12 公布
- winXP开始菜单中的历史记录怎么清除?
- kubernetes-1.0.3集群安装部署
- eclipse中快速打开Genymotion插件
- Android Jni层调用应用层(Java)的方法
- Android IOS WebRTC 音视频开发总结(五七)-- 网络传输上的一种QoS方案
- java 深入技术八(内省)
- Ubuntu14.04更新源
- Problem B: C语言习题5.14--for循环画三角形
- design 学习