在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ Given the coordinates of four points in 2D space, return whether the four points could construct a square. The coordinate (x,y) of a point is represented by an integer array with two integers. Example: Input: p1 = [0,0], p2 = [1,1], p3 = [1,0], p4 = [0,1] Output: True Note:
给定二维空间中四点的坐标,返回四点是否可以构造一个正方形。 一个点的坐标(x,y)由一个有两个整数的整数数组表示。 示例: 输入: p1 = [0,0], p2 = [1,1], p3 = [1,0], p4 = [0,1] 输出: True 注意:
Runtime: 8 ms
Memory Usage: 19.2 MB
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 } 12ms 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 }
|
请发表评论