[Swift]LeetCode1037. 有效的回旋镖 | Valid Boomerang
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.
SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。Example 1:
Input: [[1,1],[2,3],[3,2]]
Output: true
Example 2:
Input: [[1,1],[2,2],[3,3]]
Output: false
Note:
points.length == 3
points[i].length == 2
0 <= points[i][j] <= 100
回旋镖定义为一组三个点,这些点各不相同且不在一条直线上。
给出平面上三个点组成的列表,判断这些点是否可以构成回旋镖。
示例 1:
输入:[[1,1],[2,3],[3,2]] 输出:true
示例 2:
输入:[[1,1],[2,2],[3,3]] 输出:false
提示:
points.length == 3
points[i].length == 2
0 <= points[i][j] <= 100
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 }

更多精彩