• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

[Swift]LeetCode223.矩形面积|RectangleArea

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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

热烈欢迎,请直接点击!!!

进入博主App Store主页,下载使用各个作品!!!

注:博主将坚持每月上线一个新app!!!

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.

Example:

Input: A = 2
Output: 45

Note:

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


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

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

示例:

输入: -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 axis
20             return Rectangle.zero
21         }
22 
23         if r2.yr < r1.yl || r2.yl > r1.yr {
24             // No intersection on the Y axis
25             return Rectangle.zero
26         }
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 intersection
32     }
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.area
40     }
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 sum
10         } 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 + area2
11         }
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 0
13         }
14         if (A <= C && B >= D){
15             return D-C
16         }
17         if (C<=A && D>=B) {
18             return B-A
19         }
20         return (B-A) + (D-C) - max(D-A, B-C) > 0 ? (B-A) + (D-C) - max(D-A, B-C) : 0
21     }
22 }

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
[Swift]使用Playground发布时间:2022-07-13
下一篇:
Swift-mutating关键字的使用发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap