LeetCode-1232. Dash into a line

description

There are some points in an XY coordinate system. We use the array coordinates to record their coordinates respectively, where coordinates[i] = [x, y] represents the point with the abscissa x and the ordinate y.

Please judge whether these points are on the same straight line in the coordinate system. If yes, return true, otherwise, return false.

 

Example 1:

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

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

prompt:

2 <= coordinates.length <= 1000
coordinates[i].length == 2
-10^4 <= coordinates[i][0], coordinates[i][1] <= 10^4
coordinates 中不含重复的点

Source: LeetCode
Link: https://leetcode-cn.com/problems/check-if-it-is-a-straight-line/

 

Solve

Method two reference   https://leetcode-cn.com/problems/check-if-it-is-a-straight-line/solution/san-dian-xiang-chai-zhi-bi-li-chao-99-by -co4x/

    struct PairPointsCompare {
        bool operator()(const vector<int> &lhs, const vector<int> &rhs) {
            if (lhs[0] != rhs[0]) {
                return lhs[0] < rhs[0];
            }
            return lhs[1] < rhs[1];
        }
    };

    class Solution {
    public:
        // 方法一,暴力解法,对点按照X轴排序后依次对两点求取夹角
        bool checkStraightLine_1e(vector<vector<int>> &coordinates) {
            std::sort(coordinates.begin(), coordinates.end(), PairPointsCompare());
            int l = 0;
            int r = coordinates.size() - 1;
            double angle = accAngel(coordinates[l], coordinates[r]);
            const double DIFF = 1E-6;
            while (++l < --r) {
                double diff = accAngel(coordinates[l], coordinates[r]) - angle;
                if ((diff < -DIFF) || (diff > DIFF)) {
                    return false;
                }
            }
            if (l == r) {
                double diff = accAngel(coordinates[0], coordinates[l]) - angle;
                return (diff > -DIFF) && (diff < DIFF);
            }
            return true;
        }

        // 方法二,通过斜率相同转换公式,三个点数据进行比较,去除浮点数计算
        bool checkStraightLine(vector<vector<int>> &coordinates) {
            const int n = coordinates.size();
            for (int i = 1; i < n - 1; ++i) {
                // 如果只有两个点,一定是一条直线,所以不进入该循环没有任何问题
                if (((coordinates[i + 1][1] - coordinates[i][1]) * (coordinates[i][0] - coordinates[i - 1][0])) !=
                    ((coordinates[i][1] - coordinates[i - 1][1]) * (coordinates[i + 1][0] - coordinates[i][0]))) {
                    return false;
                }
            }
            return true;
        }

    private:
        inline double accAngel(const vector<int> &point1, const vector<int> &point2) {
            int xDiff = point2[0] - point1[0];
            if (xDiff == 0) {
                return 720.0; // 给一个无效夹角值
            }
            return double(point2[1] - point1[1]) / xDiff;
        }
    };

 

Guess you like

Origin blog.csdn.net/u010323563/article/details/112734105