LeetCode C++ 1232. Check If It Is a Straight Line 【Geometry/Math】简单

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.

Example 1:
LeetCode C++ 1232. Check If It Is a Straight Line 【Geometry/Math】简单_第1张图片

Input: coordinates = [[1,2],[2,3],[3,4],[4,5],[5,6],[6,7]]
Output: true

Example 2:
LeetCode C++ 1232. Check If It Is a Straight Line 【Geometry/Math】简单_第2张图片

Input: coordinates = [[1,1],[2,2],[3,4],[4,5],[5,6],[7,7]]
Output: false

Constraints:

  • 2 <= coordinates.length <= 1000 coordinates[i].length == 2
  • -10^4 <= coordinates[i][0], coordinates[i][1] <= 10^4
  • coordinates contains no duplicate point.

题意:判断给出的这些坐标点是否形成一条直线。

思路:题目中说明没有重复点,不然会更麻烦;计算斜率,可能出现除以零的情况,为此,将斜率公式变形即可。

代码:

class Solution {
public:
    bool checkStraightLine(vector<vector<int>>& coordinates) {
        //   (y1-y0)/(x1-x0) = (yi-y0)/(xi-x0)
        //-> (y1-y0)*(xi-x0) = (yi-y0)*(x1-x0)
        //循环检查其他点是否处于第一个和第二个点形成的直线上
        auto &c0 = coordinates[0], &c1 = coordinates[1];
        for (int i = 2; i < coordinates.size(); ++i) {
            auto &c2 = coordinates[i];
            if ((c1[1] - c0[1])*(c2[0] - c0[0]) != (c2[1] - c0[1])*(c1[0] - c0[0]))
                return false;
        }
        return true;
    }
};

效率:

执行用时:16 ms, 在所有 C++ 提交中击败了76.42% 的用户
内存消耗:10.3 MB, 在所有 C++ 提交中击败了100.00% 的用户

你可能感兴趣的:(LeetCode,数学,计算几何)