在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ A boomerang is a set of 3 points that are all distinct and not in a straight line. Given a list of three points in the plane, return whether these points are a boomerang. Example 1: Input: [[1,1],[2,3],[3,2]]
Output: true
Example 2: Input: [[1,1],[2,2],[3,3]]
Output: false
Note:
回旋镖定义为一组三个点,这些点各不相同且不在一条直线上。 给出平面上三个点组成的列表,判断这些点是否可以构成回旋镖。 示例 1: 输入:[[1,1],[2,3],[3,2]] 输出:true 示例 2: 输入:[[1,1],[2,2],[3,3]] 输出:false 提示:
8ms
1 class Solution { 2 func isBoomerang(_ points: [[Int]]) -> Bool { 3 let x1 = points[0][0] 4 let x2 = points[0][1] 5 let y1 = points[1][0] 6 let y2 = points[1][1] 7 let z1 = points[2][0] 8 let z2 = points[2][1] 9 10 // A/B=C/D => AD=BC 11 let kXY = (x1 - y1) * (x2 - z2) 12 let kXZ = (x2 - y2) * (x1 - z1) 13 return kXY != kXZ 14 } 15 } Runtime: 12 ms Memory Usage: 19.2 MB
1 class Solution { 2 func isBoomerang(_ points: [[Int]]) -> Bool { 3 let set:Set<[Int]> = Set(points) 4 if set.count != points.count 5 { 6 return false 7 } 8 let point1:[Int] = points[0] 9 let point2:[Int] = points[1] 10 let point3:[Int] = points[2] 11 return getSlope(point1, point2) != getSlope(point2, point3) 12 } 13 14 func getSlope(_ point1:[Int],_ point2:[Int]) -> Double 15 { 16 return Double(point2[1] - point1[1]) / Double(point2[0] - point1[0]) 17 } 18 } 12ms
1 class Solution { 2 func isBoomerang(_ points: [[Int]]) -> Bool { 3 var area = points[0][0]*(points[1][1]-points[2][1])+points[1][0]*(points[2][1]-points[0][1])+points[2][0]*(points[0][1]-points[1][1]); 4 return area != 0; 5 } 6 }
|
请发表评论