在线时间:8:00-16:00
迪恩网络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 }
|
请发表评论