Leetcode - check-if-it-is-a-straight-line
https://leetcode.com/problems/check-if-it-is-a-straight-line/
You are given an array coordinates, coordinates[i] = [x, y], where [x, y] represents the coordinate of a point. Check if these points make a straight line in the XY plane.
Input: coordinates = [[1,2],[2,3],[3,4],[4,5],[5,6],[6,7]] Output: true
Input: coordinates = [[1,1],[2,2],[3,4],[4,5],[5,6],[7,7]] Output: false
# @param {Integer[][]} coordinates
# @return {Boolean}
def check_straight_line(points)
k = (points[1][1] - points[0][1]).to_f / (points[1][0] - points[0][0]).to_f
for i in 2..points.length-1
tmp_k = (points[i][1] - points[i-1][1]).to_f / (points[i][0] - points[i-1][0]).to_f
return false if tmp_k != k
end
return true
end
func checkStraightLine(points [][]int) bool {
k := float64(points[1][1] - points[0][1]) / float64(points[1][0] - points[0][0])
for i := 2; i < len(points); i++ {
tmpK := float64(points[i][1] - points[i-1][1]) / float64(points[i][0] - points[i-1][0])
if tmpK != k {
return false
}
}
return true
}
评论 (0)