为有牺牲多壮志,敢教日月换新天。

[Swift]LeetCode1037. 有效的回旋镖 | Valid Boomerang

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/10810810.html 
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

热烈欢迎,请直接点击!!!

进入博主App Store主页,下载使用各个作品!!!

注:博主将坚持每月上线一个新app!!!

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. points.length == 3
  2. points[i].length == 2
  3. 0 <= points[i][j] <= 100

回旋镖定义为一组三个点,这些点各不相同且不在一条直线上。

给出平面上三个点组成的列表,判断这些点是否可以构成回旋镖。

示例 1:

输入:[[1,1],[2,3],[3,2]]
输出:true

示例 2:

输入:[[1,1],[2,2],[3,3]]
输出:false

提示:

  1. points.length == 3
  2. points[i].length == 2
  3. 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 }

 

posted @ 2019-05-05 08:17  为敢技术  阅读(313)  评论(0编辑  收藏  举报