博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[Swift]LeetCode223. 矩形面积 | Rectangle Area
阅读量:5107 次
发布时间:2019-06-13

本文共 4714 字,大约阅读时间需要 15 分钟。

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝()
➤GitHub地址:
➤原文地址: 
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

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

Example:

Input: A = -3, B = 0, C = 3, D = 4, E = 0, F = -1, G = 9, H = 2 Output: 45

Note:

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


在二维平面上计算出两个由直线构成的矩形重叠后形成的总面积。

每个矩形由其左下顶点和右上顶点坐标表示,如图所示。

Rectangle Area

示例:

输入: -3, 0, 3, 4, 0, -1, 9, 2输出: 45

说明: 假设矩形面积不会超出 int 的范围。


32ms

1 class Solution { 2     struct Rectangle { 3         var xl: Int 4         var yl: Int 5         var xr: Int 6         var yr: Int 7  8         static var zero: Rectangle { 9             return Rectangle(xl: 0, yl: 0, xr: 0, yr: 0)10         }11 12         var area: Int {13             return (xr - xl) * (yr - yl)14         }15     }16 17     private func computeIntersetion(r1: Rectangle, r2: Rectangle) -> Rectangle {18         if r2.xr < r1.xl || r2.xl > r1.xr {19             // No intersection on the X axis20             return Rectangle.zero21         }22 23         if r2.yr < r1.yl || r2.yl > r1.yr {24             // No intersection on the Y axis25             return Rectangle.zero26         }27 28         let intersection = Rectangle(xl: max(r1.xl, r2.xl), yl: max(r1.yl, r2.yl),29                                      xr: min(r1.xr, r2.xr), yr: min(r1.yr, r2.yr))30 31         return intersection32     }33 34     func computeArea(_ A: Int, _ B: Int, _ C: Int, _ D: Int, _ E: Int, _ F: Int, _ G: Int, _ H: Int) -> Int {35         let r1 = Rectangle(xl: A, yl: B, xr: C, yr: D)36         let r2 = Rectangle(xl: E, yl: F, xr: G, yr: H)37         let intersection = computeIntersetion(r1: r1, r2: r2)38 39         return r1.area + r2.area - intersection.area40     }41 }

36ms

1 class Solution {2     func computeArea(_ A: Int, _ B: Int, _ C: Int, _ D: Int, _ E: Int, _ F: Int, _ G: Int, _ H: Int) -> Int {3 4         var sum = (C-A)*(D-B)+(G-E)*(H-F)5         if E>=C || H<=B || F>=D || G<=A { return sum }6         return sum - (min(G, C) - max(A, E)) * (min(D, H) - max(B, F))7     }8 }

40ms

1 class Solution { 2     func computeArea(_ A: Int, _ B: Int, _ C: Int, _ D: Int, _ E: Int, _ F: Int, _ G: Int, _ H: Int) -> Int { 3  4         let area1 = (C - A) * (D - B) 5         let area2 = (G - E) * (H - F) 6         let sum = area1 + area2 7          8         if A >= G || C <= E || B >= H || D <= F { 9             return sum10         } else {11             let x = [A,C,E,G].sorted(by: < )12             let w = x[2] - x[1]13             let y = [B,D,F,H].sorted(by: < )14             let h = y[2] - y[1]15             16             return sum - (w*h)17         }18 19     }20 }

40ms

1 class Solution { 2     func computeArea(_ A: Int, _ B: Int, _ C: Int, _ D: Int, _ E: Int, _ F: Int, _ G: Int, _ H: Int) -> Int { 3         let area1 = abs(A - C) * abs(B - D) 4         let area2 = abs(E - G) * abs(F - H) 5         let area3 = abs(min(C, G) - max(A, E)) * abs(min(D, H) - max(B, F)) 6         if ((E <= A && A <= G) || (E <= C && C <= G) || (A <= E && E <= C) || (A <= G && G <= C)) 7         && ((F <= B && B <= H) || (F <= D && D <= H) || (B <= F && F <= D) || (B <= H && H <= D)) {   8             return area1 + area2 - area3 9         } else {10             return area1 + area211         }12     }13 }

68ms

1 class Solution { 2     func computeArea(_ A: Int, _ B: Int, _ C: Int, _ D: Int, _ E: Int, _ F: Int, _ G: Int, _ H: Int) -> Int { 3          if A > C || B > D || E > G || F > H { 4             return 0 5         } 6         let area = (C - A) * (D - B) + (G - E) * (H - F) 7         if max(A, E) > min(C, G) || max(B, F) > min(D, H){ 8             return area 9         }10         return area - (min(C, G) - max(A, E)) * (min(D, H) - max(B, F))11     }12 }

92ms

1 class Solution { 2     func computeArea(_ A: Int, _ B: Int, _ C: Int, _ D: Int, _ E: Int, _ F: Int, _ G: Int, _ H: Int) -> Int { 3         let x = overlap(A, C, E, G) 4         let y = overlap(B, D, F, H) 5         let first = (D-B)*(C-A) 6         let sec = (G-E)*(H-F) 7         return first + sec - x*y 8     } 9     10     func overlap(_ A: Int,_ B: Int,_ C: Int,_ D: Int) -> Int {11         if A == B || C == D {12             return 013         }14         if (A <= C && B >= D){15             return D-C16         }17         if (C<=A && D>=B) {18             return B-A19         }20         return (B-A) + (D-C) - max(D-A, B-C) > 0 ? (B-A) + (D-C) - max(D-A, B-C) : 021     }22 }

 

转载于:https://www.cnblogs.com/strengthen/p/10203093.html

你可能感兴趣的文章
性能测试工具 java ur_jmeter之自定义java请求性能测试
查看>>
java 成员域_java – Eclipse为域对象和带有’m’后缀的类成员生成getter/setter
查看>>
java jute_ZooKeeper源码阅读(六):JUTE
查看>>
java 创建书类_1、创建实体类(Customer.java、Orders.java)复制代码 代码如下:package wck.stu.vo.oneToMany_single...
查看>>
java短信验证怎么实现6,如何实现java手机短信验证功能
查看>>
php 5.2 xdebug,用PHP 5.5安装xdebug
查看>>
php多克,在PHP中使用Heredoc有什么好处?
查看>>
安卓setText导入什么Java包,java – 如何在recyclerview上添加SetOnClicklistener
查看>>
php 整数转日期,php数字转时间的方法
查看>>
mle matlab,MLE的Matlab程序
查看>>
php 视频资源互动共享系统,PHP教与学资源网站设计
查看>>
php buffer 切割,PHP 输出buffer控制 | 学步园
查看>>
php 名称搜索排序,php – 按名称弹性搜索排序
查看>>
java pdf动态生成,从Java应用程序动态生成PDF文件
查看>>
红细胞识别matlab,图像处理—红细胞计数(Matlab).doc
查看>>
php让from背景变成半透明,php – imagecreatefrompng()使一个黑色的背景,而不是透明?...
查看>>
oracle函数基础知识,ORACLE 基础知识以及基本函数
查看>>
oracle9i安装后,Oracle9i安装过程说明
查看>>
oracle+609,Fatal NI Connect 12560' And 'ORA-609 解决方法
查看>>
oracle会话比进程高,oracle数据库CPU特别高的解决方法详解
查看>>