★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-wscgqvff-mc.html
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html
A boomerang is a set of 3 points that are all distinct and not in a straight line.git
Given a list of three points in the plane, return whether these points are a boomerang.github
Example 1:微信
Input: [[1,1],[2,3],[3,2]]
Output: true
Example 2:spa
Input: [[1,1],[2,2],[3,3]]
Output: false
Note:code
points.length == 3
points[i].length == 2
0 <= points[i][j] <= 100
迴旋鏢定義爲一組三個點,這些點各不相同且不在一條直線上。htm
給出平面上三個點組成的列表,判斷這些點是否能夠構成迴旋鏢。blog
示例 1:three
輸入:[[1,1],[2,3],[3,2]] 輸出:true
示例 2:get
輸入:[[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 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
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 }
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 }