[Swift]LeetCode1037. 有效的回旋镖 | Valid Boomerang
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/10810810.html
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
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:
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
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 }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了