Code Review Swift 演算法題: 最小面積矩形 Leetcode 的動人之處
對於陣列中的每一對點,設想他們是一個矩形的對角線,然後就簡單了。
矩形有兩條對角線,如果另外一條對角線上面的點,也在給定的數組裡面,就找出了一個滿足要求的矩形。
用雜湊集合確認四個點。
舉個例子:
有了兩個點 (1, 1) 和 (5, 5) 。看一下 (1, 5) 和 (5, 1) 有沒有。
有,就找到了一個滿足要求的矩形。 然後,找出所有的矩形中,面積最小的。
演算法這麼走:
把所有的點,放入一個雜湊集合。
對於每一對點,如果雜湊集合 set 中包含,相關矩形四個不同的頂點,
( 換句話說, 交換下 x 與 y, 如果能在雜湊集合中找到另一條對角線的兩個點 )
該矩形的面積是,一個可能的解。
題解 ( 改進前):
因為 Swift 中的元組沒實現雜湊協議,
(Python 中的元組,自帶雜湊)
所以要用雜湊集合,就要實現座標的結構體。
我參照了一下這個 StackOverFlow 的連結, 就寫出了下面的。
這麼寫,效能比較差,Leetcode 報超時: Time Limit Exceeded
var hashValue: Int{ return "(\(x),\(y))".hashValue } 複製程式碼
根據題目的限制,我改進了一下雜湊的 get 屬性,就通過了
var hashValue: Int{ return x * 100000 + y } 複製程式碼
( 關於 Leetcode 用 Swift 語言答題的, 報超時另一經驗是,遍歷字串的時候,先把字串轉化為陣列。
Swift 遍歷陣列的效能,要好一些 )
改進前
// 為了利用雜湊集合,構建結構體 struct Point: Hashable{ var x: Int var y: Int init(_ x: Int, _ y: Int) { self.x = x self.y = y } var hashValue: Int{ return x * 100000 + y } static func == (_ lhs: Point, _ rhs: Point) -> Bool{ return lhs.x == rhs.x && lhs.y == rhs.y } } func minAreaRect(_ points: [[Int]]) -> Int { let newPoints = points.map({ (point: [Int]) -> Point in return Point(point[0], point[1]) }) // 先把所有有效的點找出來 ( 就是,沒有重複的 ) let pointSet = Set(newPoints) var minArea = Int.max //然後兩次迴圈,每一對點,都嘗試搭配一次,找出每一個可能的矩形 for point in points{ for innerPoint in points{ if point[0] != innerPoint[0] , point[1] != innerPoint[1] , pointSet.contains(Point(point[0], innerPoint[1])) ,pointSet.contains(Point(innerPoint[0], point[1])) { // 找出最小的矩形 minArea = min(minArea, abs((innerPoint[1] - point[1] ) * (innerPoint[0] - point[0]))) } } } if minArea == Int.max { return 0 } else{ return minArea } } 複製程式碼
結構體有些冗餘,不但實現了 Hashable, 還實現了 Hashable 派生出來的 Equatable 協議
Code Review:
演算法上的改進 ( 使用數學提升效能, 初中的 )
for point in points{ for innerPoint in points{ if ( // ... 判斷條件 ) { // 找出最小的矩形 minArea = min(minArea, abs((innerPoint[1] - point[1] ) * (innerPoint[0] - point[0]))) } } } 複製程式碼
根據解題思路,對角線的兩頂點。 可以設想一頂點是左下,一頂點是右上,
( 因為設想對角線的位置,決定了後面兩個點的座標怎麼取 )
右上的頂點 x , y 值自然比 左下的大,這樣就省去了取絕對值的操作。
for lowerLeft in points { for upperRight in points { if ( // ... 判斷條件 ) { let area = (upperRight[0] - lowerLeft[0]) * (upperRight[1] - lowerLeft[1]) minArea = min(minArea, area) } } } 複製程式碼
Swift 語言上的改進,
這個題目中的 Point 結構體,賦值後,就沒有再修改 (寫入)。 可以改 var
為 let
.
Swift 4.2 中,如果結構體所有的成員變數都遵守 Hashable協議,
編譯器回自動給該結構體建立 Hashable 協議的方法。
struct Point: Hashable { let x: Int let y: Int } 複製程式碼
結構體有自己預設的初始化方法,不用補充一個
改進閉包
let newPoints = points.map({ (point: [Int]) -> Point in return Point(point[0], point[1]) }) let pointSet = Set(newPoints) 複製程式碼
Swift 語言有型別推導特性,就不用顯式宣告型別了。編譯器能夠自動推匯出引數和返回值的型別
let newPoints = points.map { point in Point(x: point[0], y: point[1]) } let pointSet = Set(newPoints) 複製程式碼
經過上一步的整理,程式碼比較簡潔,可以進一步合併
let pointSet = Set(points.map { point in Point(x: point[0], y: point[1]) }) 複製程式碼
改進後的程式碼:
struct Point: Hashable { let x: Int let y: Int } func minAreaRect(_ points: [[Int]]) -> Int { let pointSet = Set(points.map { point in Point(x: point[0], y: point[1]) }) var minArea = Int.max for lowerLeft in points { for upperRight in points { if upperRight[0] > lowerLeft[0] && upperRight[1] > lowerLeft[1] && pointSet.contains(Point(x: lowerLeft[0], y: upperRight[1])) && pointSet.contains(Point(x: upperRight[0], y: lowerLeft[1])) { let area = (upperRight[0] - lowerLeft[0]) * (upperRight[1] - lowerLeft[1]) minArea = min(minArea, area) } } } return minArea == Int.max ? 0 : minArea } 複製程式碼
Leetcode 的動人之處挺多的,本文繼續 8 看程式碼的姿勢
檢視競賽回顧
( Leetcode 的競賽很強大,每個星期天都有 )
進入競賽,

下滑到競賽回顧,點選感興趣的一場 (就是找到想做的題目)

下滑,選擇更多

點選感興趣的題目

看到程式碼。 ( 程式碼這麼多,肯定看不完 )

有了 Leetcode 討論區,為什麼還推薦這樣看程式碼? ( 雖然是很強的人,寫的程式碼 )
因為這是競賽的時候寫的程式碼,很趕時間。 哪裡有後面的那麼多的設計。
很不優雅,糙,快,直觀。( 大神的程式碼思路,較容易的理解 ... )
題目做不出來,可以瞭解一下。
( 想看高手的,可以...
沒 dollar 買會員,想做題, 例如第 772 ,靠百度。這裡可以看程式碼思路,第 69 場周賽 )
Leetcode 的精華,是測試用例
測試用例多,有時候各種想不到,讓程式員的思維更加周全
例如這道題,有用例 130

這個用例,體會到了我程式碼的脆弱
if point[0] != innerPoint[0] , point[1] != innerPoint[1] , pointSet.contains(Point(innerPoint[0], point[1])) ,pointSet.contains(Point(innerPoint[1], point[0])) { // 找出最小的矩形 minArea = min(minArea, abs((innerPoint[1] - point[1] ) * (innerPoint[0] - point[0]))) } 複製程式碼
另一對頂點的語義,取反了
Leetcode 連結: ofollow,noindex">Minimum Area Rectangle
感謝Martin R code review 我的程式碼
相關程式碼: github.com/BoxDengJZ/l…