1037. 有效的回旋镖
有效的回旋镖
题目描述:
给定一个数组 points ,其中 points[i] = [xi, yi] 表示 X-Y 平面上的一个点,如果这些点构成一个 回旋镖 则返回 true 。
回旋镖 定义为一组三个点,这些点 各不相同 且 不在一条直线上 。
示例 1:
输入:points = [[1,1],[2,3],[3,2]]
输出:true
示例 2:
输入:points = [[1,1],[2,2],[3,3]]
输出:false
提示:
points.length == 3
points[i].length == 2
0 <= xi, yi <= 100
思路:
时间复杂度:O(n),空间复杂度O(1)
简单题,把情况判断清楚就好,好久没双百了
代码:
func isBoomerang(points [][]int) bool {
if points[0][0] == points[1][0] && points[1][0] == points[2][0]{
return false
}
if points[0][1] == points[1][1] && points[1][1] == points[2][1]{
return false
}
if points[0][0] == points[1][0]&&points[0][1] == points[1][1]{
return false
}
if points[1][0] == points[2][0]&&points[1][1] == points[2][1]{
return false
}
if points[0][0] == points[2][0]&&points[0][1] == points[2][1]{
return false
}
var a,b float64
a = float64(points[1][1]-points[0][1])/float64(points[1][0]-points[0][0])
b = float64(points[2][1]-points[1][1])/float64(points[2][0]-points[1][0])
if a == b{
return false
}
return true
}
代码效率:
执行用时:0 ms, 在所有 Go 提交中击败了100.00%的用户
内存消耗:1.9 MB, 在所有 Go 提交中击败了100.00%的用户
本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!