★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-neewwdjw-me.html
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html
Given the coordinates of four points in 2D space, return whether the four points could construct a square.git
The coordinate (x,y) of a point is represented by an integer array with two integers.github
Example:數組
Input: p1 = [0,0], p2 = [1,1], p3 = [1,0], p4 = [0,1] Output: True
Note:微信
給定二維空間中四點的座標,返回四點是否能夠構造一個正方形。app
一個點的座標(x,y)由一個有兩個整數的整數數組表示。ide
示例:spa
輸入: p1 = [0,0], p2 = [1,1], p3 = [1,0], p4 = [0,1] 輸出: True
注意:code
1 class Solution { 2 func validSquare(_ p1: [Int], _ p2: [Int], _ p3: [Int], _ p4: [Int]) -> Bool { 3 var s:Set<Int> = [d(p1, p2), d(p1, p3), d(p1, p4), d(p2, p3), d(p2, p4), d(p3, p4)] 4 return !s.contains(0) && s.count == 2 5 } 6 7 func d(_ p1:[Int],_ p2:[Int]) -> Int 8 { 9 return (p1[0] - p2[0]) * (p1[0] - p2[0]) + (p1[1] - p2[1]) * (p1[1] - p2[1]) 10 } 11 }
12mshtm
1 class Solution { 2 func validSquare(_ p1: [Int], _ p2: [Int], _ p3: [Int], _ p4: [Int]) -> Bool { 3 var dict = [Int: Int]() 4 let distances = [dist(p1, p2), dist(p1, p3), dist(p1, p4), dist(p2, p3), dist(p2, p4), dist(p3, p4)] 5 var maxDist = -1 6 for dist in distances { 7 maxDist = max(maxDist, dist) 8 dict[dist] = dict[dist, default: 0] + 1 9 } 10 if (dict[maxDist] == 2 && dict.count == 2) { 11 return true 12 } 13 return false 14 } 15 16 func dist(_ p1: [Int], _ p2: [Int]) -> Int { 17 return (p1[0]-p2[0]) * (p1[0]-p2[0]) + (p1[1]-p2[1]) * (p1[1]-p2[1]) 18 } 19 }
16ms
1 import Foundation 2 3 class Solution { 4 func validSquare(_ p1: [Int], _ p2: [Int], _ p3: [Int], _ p4: [Int]) -> Bool { 5 let points : [[Int]] = [p1,p2,p3,p4] 6 var totalsum = 0 7 var sums : [Double] = [] 8 9 if Set(points).count != points.count { 10 return false 11 } 12 13 func get_distance(_ d1:[Int],_ d2: [Int]) -> Double { 14 let dist = sqrt(pow((Double(d1[0])-Double(d2[0])),2)+pow((Double(d1[1])-Double(d2[1])),2)) 15 return dist 16 } 17 18 for i in Range(0...points.count - 1) { 19 if i == points.count - 1 { 20 break 21 } 22 for j in Range(i+1...points.count - 1) { 23 let d = get_distance(points[i],points[j]) 24 sums.append(d) 25 } 26 } 27 if Set(sums).count == 2 { 28 return true 29 } else { 30 return false 31 } 32 } 33 }
20ms
1 class Solution { 2 func validSquare(_ p1: [Int], _ p2: [Int], _ p3: [Int], _ p4: [Int]) -> Bool { 3 4 var distanceArray: [Int] = [] 5 6 distanceArray.append(distanceBetween(p1: p1, p2: p2)) 7 distanceArray.append(distanceBetween(p1: p1, p2: p3)) 8 distanceArray.append(distanceBetween(p1: p1, p2: p4)) 9 distanceArray.append(distanceBetween(p1: p2, p2: p3)) 10 distanceArray.append(distanceBetween(p1: p2, p2: p4)) 11 distanceArray.append(distanceBetween(p1: p3, p2: p4)) 12 13 distanceArray.sort() 14 15 if distanceArray[0] > 0 16 , distanceArray[0] == distanceArray[1] 17 , distanceArray[1] == distanceArray[2] 18 , distanceArray[2] == distanceArray[3] 19 , distanceArray[4] == distanceArray[5] { 20 return true 21 } else { 22 return false 23 } 24 } 25 26 private func distanceBetween(p1: [Int], p2: [Int]) -> Int { 27 let deltaX = p2.first! - p1.first! 28 let deltaY = p2.last! - p1.last! 29 return deltaX * deltaX + deltaY * deltaY 30 } 31 }