[Swift]LeetCode391. 完美矩形 | Perfect Rectangle

Given N axis-aligned rectangles where N > 0, determine if they all together form an exact cover of a rectangular region.

Each rectangle is represented as a bottom-left point and a top-right point. For example, a unit square is represented as [1,1,2,2]. (coordinate of bottom-left point is (1, 1) and top-right point is (2, 2)).

Example 1:

rectangles = [
  [1,1,3,3],
  [3,1,4,2],
  [3,2,4,4],
  [1,3,2,4],
  [2,3,3,4]
]

Return true. All 5 rectangles together form an exact cover of a rectangular region. 

Example 2:

rectangles = [
  [1,1,2,3],
  [1,3,2,4],
  [3,1,4,2],
  [3,2,4,4]
]

Return false. Because there is a gap between the two rectangular regions. 

Example 3:

rectangles = [
  [1,1,3,3],
  [3,1,4,2],
  [1,3,2,4],
  [3,2,4,4]
]

Return false. Because there is a gap in the top center. 

Example 4:

rectangles = [
  [1,1,3,3],
  [3,1,4,2],
  [1,3,2,4],
  [2,2,4,4]
]

Return false. Because two of the rectangles overlap with each other.


我们有 N 个与坐标轴对齐的矩形, 其中 N > 0, 判断它们是否能精确地覆盖一个矩形区域。

每个矩形用左下角的点和右上角的点的坐标来表示。例如, 一个单位正方形可以表示为 [1,1,2,2]。 ( 左下角的点的坐标为 (1, 1) 以及右上角的点的坐标为 (2, 2) )。

示例 1:

rectangles = [
  [1,1,3,3],
  [3,1,4,2],
  [3,2,4,4],
  [1,3,2,4],
  [2,3,3,4]
]

返回 true。5个矩形一起可以精确地覆盖一个矩形区域。 

示例 2:

rectangles = [
  [1,1,2,3],
  [1,3,2,4],
  [3,1,4,2],
  [3,2,4,4]
]

返回 false。两个矩形之间有间隔,无法覆盖成一个矩形。

示例 3:

rectangles = [
  [1,1,3,3],
  [3,1,4,2],
  [1,3,2,4],
  [3,2,4,4]
]

返回 false。图形顶端留有间隔,无法覆盖成一个矩形。

示例 4:

rectangles = [
  [1,1,3,3],
  [3,1,4,2],
  [1,3,2,4],
  [2,2,4,4]
]

返回 false。因为中间有相交区域,虽然形成了矩形,但不是精确覆盖。 


524ms

 1 class Solution {
 2     func isRectangleCover(_ rectangles: [[Int]]) -> Bool {
 3         var st:Set<String> = Set<String>()
 4         var min_x = Int.max
 5         var min_y = Int.max
 6         var max_x = Int.min
 7         var max_y = Int.min
 8         var area:Int = 0
 9         for rect in rectangles
10         {
11             min_x = min(min_x, rect[0])
12             min_y = min(min_y, rect[1])
13             max_x = max(max_x, rect[2])
14             max_y = max(max_y, rect[3])
15             area += (rect[2] - rect[0]) * (rect[3] - rect[1])
16             var s1:String = String(rect[0]) + "_" + String(rect[1]) // bottom-left
17             var s2:String = String(rect[0]) + "_" + String(rect[3]) // top-left
18             var s3:String = String(rect[2]) + "_" + String(rect[3]) // top-right
19             var s4:String = String(rect[2]) + "_" + String(rect[1]) // bottom-right
20             if st.contains(s1) {st.remove(s1)}
21             else {st.insert(s1)}
22             if st.contains(s2) {st.remove(s2)}
23             else {st.insert(s2)}
24             if st.contains(s3) {st.remove(s3)}
25             else {st.insert(s3)}
26             if st.contains(s4) {st.remove(s4)}
27             else {st.insert(s4)}
28         }
29         var t1:String = String(min_x) + "_" + String(min_y)
30         var t2:String = String(min_x) + "_" + String(max_y)
31         var t3:String = String(max_x) + "_" + String(max_y)
32         var t4:String = String(max_x) + "_" + String(min_y)
33         if !st.contains(t1) || !st.contains(t2) || !st.contains(t3) || !st.contains(t4) || st.count != 4
34         {
35             return false
36         }
37         return area == (max_x - min_x) * (max_y - min_y)
38     }
39 }


1496ms

 1 class Solution {
 2             func isRectangleCover(_ rectangles: [[Int]]) -> Bool {
 3         var dic = Dictionary<String,Int>()
 4         var commenDic = Dictionary<String,Int>()
 5         //计算面积(排除覆盖)
 6         var minX = rectangles[0][0]
 7         var minY = rectangles[0][1]
 8         var maxX = rectangles[0][2]
 9         var maxY = rectangles[0][3]
10         var area:Int = 0
11         //计算角的个数
12         for i in rectangles{
13             if i[0] < minX {
14                 minX = i[0]
15             }
16             if i[1] < minY {
17                 minY = i[1]
18             }
19             if i[2] > maxX {
20                 maxX = i[2]
21             }
22             if i[3] > maxY {
23                 maxY = i[3]
24             }
25             area += (i[2]-i[0]) * (i[3]-i[1])
26
27             let p1 = String(i[0])+","+String(i[1])
28             let p2 = String(i[0])+","+String(i[3])
29
30             let p3 = String(i[2])+","+String(i[3])
31             let p4 = String(i[2])+","+String(i[1])
32             let pArr = [p1,p2,p3,p4]
33             for p in pArr{
34                 if let _ = dic[p]{
35                     dic[p] = nil
36                 }else{
37                     dic[p] = 1
38                 }
39             }
40             //去除遮挡
41             let p11 = p1 + ",1"
42             let p21 = p2 + ",2"
43             let p31 = p3 + ",3"
44             let p41 = p4 + ",4"
45             let pArr2 = [p11,p21,p31,p41]
46             for p in pArr2{
47                 if let _ = commenDic[p]{
48                     return false
49                 }else{
50                     commenDic[p] = 1
51                 }
52             }
53         }
54         if dic.count != 4{
55             return false
56         }
57         if area != (maxY - minY) * (maxX - minX){
58             return false
59         }
60         return true
61     }
62 }

原文地址:https://www.cnblogs.com/strengthen/p/10299478.html

时间: 2024-10-11 13:30:08

[Swift]LeetCode391. 完美矩形 | Perfect Rectangle的相关文章

LeetCode 223. 矩形面积(Rectangle Area)

223. 矩形面积 223. Rectangle Area 题目描述 在二维平面上计算出两个由直线构成的矩形重叠后形成的总面积. 每个矩形由其左下顶点和右上顶点坐标表示,如图所示. LeetCode223. Rectangle Area中等 示例: 输入: -3, 0, 3, 4, 0, -1, 9, 2 输出: 45 说明: 假设矩形面积不会超出 int 的范围. Java 实现 class Solution { public int computeArea(int A, int B, int

391 Perfect Rectangle 完美矩形

有 N 个与坐标轴对齐的矩形, 其中 N > 0, 判断它们是否能精确地覆盖一个矩形区域.每个矩形用左下角的点和右上角的点的坐标来表示.例如, 一个单位正方形可以表示为 [1,1,2,2]. ( 左下角的点的坐标为 (1, 1) 以及右上角的点的坐标为 (2, 2) ). 详见:https://leetcode.com/problems/perfect-rectangle/description/ C++: class Solution { public: bool isRectangleCov

Perfect Rectangle(完美矩形)

我们有 N 个与坐标轴对齐的矩形, 其中 N > 0, 判断它们是否能精确地覆盖一个矩形区域. 每个矩形用左下角的点和右上角的点的坐标来表示.例如, 一个单位正方形可以表示为 [1,1,2,2]. ( 左下角的点的坐标为 (1, 1) 以及右上角的点的坐标为 (2, 2) ). 示例 1: rectangles = [ [1,1,3,3], [3,1,4,2], [3,2,4,4], [1,3,2,4], [2,3,3,4] ] 返回 true.5个矩形一起可以精确地覆盖一个矩形区域. 示例 2

[Swift]LeetCode836. 矩形重叠 | Rectangle Overlap

A rectangle is represented as a list [x1, y1, x2, y2], where (x1, y1) are the coordinates of its bottom-left corner, and (x2, y2) are the coordinates of its top-right corner. Two rectangles overlap if the area of their intersection is positive.  To b

391. Perfect Rectangle

Given N axis-aligned rectangles where N > 0, determine if they all together form an exact cover of a rectangular region. Each rectangle is represented as a bottom-left point and a top-right point. For example, a unit square is represented as [1,1,2,2

Leetcode: Perfect Rectangle

Given N axis-aligned rectangles where N > 0, determine if they all together form an exact cover of a rectangular region. Each rectangle is represented as a bottom-left point and a top-right point. For example, a unit square is represented as [1,1,2,2

6-1 设计一个矩形类Rectangle (10分)

设计一个名为Rectangle的类表示矩形.这个类包括: 两个名为width和height的double型数据域,它们分别表示矩形的宽和高.width和height的默认值都为1. 一个无参构造方法. 一个为width和height指定值的矩形构造方法. 一个名为getArea()的方法返回这个矩形的面积. 一个名为getPerimeter()的方法返回这个矩形的周长. 类名为: Rectangle 裁判测试程序样例: import java.util.Scanner; /* 你的代码将被嵌入到

定义抽象类Shape,抽象方法为showArea(),求出面积并显示,定义矩形类Rectangle,正方形类Square,圆类 Circle,根据各自的属性,用showArea方法求出各自的面积,在main方法中构造3个对象,调用showArea方法。(体现多态)

实现多态的三个条件:1.要有继承2.要有抽象方法重写3.用父类指针(引用)指向子类对象 重载重写重定义的区别: 1.重载:在同一个类中进行; 编译时根据参数类型和个数决定方法调用; 子类无法重载父类; 父类同名方法被子类该方法覆盖. 2.重写:在父类和子类之间进行; 父类与子类方法有完全相同类型; 在运行时根据具体对象类型决定方法调用; 3.在重写中有抽象方法的会产生多态;没有使用抽象方法叫重定义 以下具体代码具体分析: package test3;abstract class Shape{ /

iOS开发笔记 - 语言篇之Swift

?2014年的苹果全球开发者大会(WWDC),当Craig Federighi向全世界宣布"We have new programming language"(我们有了新的编程语言)的时候,全场响起了最热烈和持久的掌声,伴随着掌声到来的语言叫Swift.接下来Craig Federighi更是毫不掩饰的告诉大家,Swift将成为主宰iOS和Mac开发的新语言,甚至是整个软件行业中最举足轻重的语言. ??Swift正如它的名字那样迅速.敏捷,但这并不是它的全部.Swift是一个博采众长的