LeetCode1232---dotted into a line

    I gained a little from doing the questions on LeetCode today, so I will record it here. It is a simple question, a simple description, it gives me some points, we judge whether these points are all in a straight line. (See title and link at the end of the article).

    The first thing I consider is to first find the slope of the first two points, and then find the slope of all the following points and the first point respectively, and compare whether they are equal, and return false directly if they are not equal. If they are all equal at the end, return true. Here, if the slope is required, division is required, and division needs to determine whether the numerator is 0. So my specific method is as follows:

    1. Determine whether the abscissa of the first point and the second point are the same. If same, go to 2. If not, go to 3.
    2. Determine whether the abscissa coordinates of the remaining points are the same as the first point, and return false if they are not the same halfway. Otherwise return true when finished. 3. Calculate the slope K of the first point and the second point, and calculate the slope K i K_i
    of the remaining points and the first point respectivelyKiAnd judge whether it is equal to K. If it is not equal halfway, it returns false, otherwise it returns true after the end.

My solution is as follows:

class Solution {
    
    
public:
    bool checkStraightLine(vector<vector<int>>& coordinates) {
    
    
        int len = coordinates.size();
        if(len <= 2)
            return true;
        if(coordinates[0][0] == coordinates[1][0])//如果前两个数x相同,则判读后面的数x是否都和第一个相同
        {
    
    
            for(int i = 2;i < len;++i)
                if(coordinates[i][0] != coordinates[0][0])
                    return false;
        }
        else
        {
    
    
            //计算两点的斜率,然后判断后面每个点和第一个点的斜率,如果出现不相同则false
            float k = (coordinates[1][1] - coordinates[0][1]) * 1.0 / (coordinates[1][0] - coordinates[0][0]);
            for(int i = 2;i < len;++i)
            {
    
    
                if((coordinates[i][1] - coordinates[0][1]) * 1.0 / (coordinates[i][0] - coordinates[0][0]) != k)
                    return false;
            }
        }
        return true;
    }
};

    After finishing the question, I went to look at the solution, and found a very interesting method, which can avoid the judgment of division and division by 0. In fact, he just made a very clever transformation. Later, we no longer judge whether the slopes are equal, but make a deformation of the slope formula. For example, what we originally judged was ( y 2 − y 1 ) / ( x 2 − x 1 ) = = ( yi − y 1 ) / ( xi − x 1 ) (y_2 - y_1)/(x_2 - x_1) ==( y_i - y_1)/(x_i - x_1)(y2y1)/(x2x1)==(yiy1)/(xix1) , now cross-multiply it, judge( y 2 − y 1 ) ∗ ( xi − x 1 ) = = ( yi − y 1 ) ∗ ( x 2 − x 1 ) (y_2 - y_1)*(x_i - x_1 )==(y_i - y_1)*(x_2-x_1)(y2y1)(xix1)==(yiy1)(x2x1) . The following is the modified solution:

class Solution {
    
    
public:
    bool checkStraightLine(vector<vector<int>>& coordinates) {
    
    
        int len = coordinates.size();
        if(len <= 2)
            return true;
        //为了避免除法和除零的判断,可以转变为乘法,乘法效率比除法高
        int preDx = coordinates[1][0] - coordinates[0][0],preDy = coordinates[1][1] - coordinates[0][1],dx,dy;
        for(int i = 2;i < len;++i)
        {
    
    
            dx = coordinates[i][0] - coordinates[0][0];
            dy = coordinates[i][1] - coordinates[0][1]; 
            if(dy * preDx != dx * preDy)
                return false;
        }
        return true;
    }
};

    This question does not mean that the efficiency of the latter method has been greatly improved, but it has taught me to change my thinking flexibly. If I can’t do it the other way around, if I can’t do division, I can transform it into multiplication. Sometimes it’s the same when I do things. , If you have not been able to get good results in one direction, you might as well think about it in reverse , and you may have a different experience.

Original title and link:
在一个 XY 坐标系中有一些点,我们用数组 coordinates 来分别记录它们的坐标,其中 coordinates[i] = [x, y] 表示横坐标为 x、纵坐标为 y 的点。

请你来判断,这些点是否在该坐标系中属于同一条直线上,是则返回 true,否则请返回 false

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/check-if-it-is-a-straight-line

Example one:
insert image description here

输入:coordinates = [[1,2],[2,3],[3,4],[4,5],[5,6],[6,7]]
输出:true

Example two:
insert image description here

输入:coordinates = [[1,1],[2,2],[3,4],[4,5],[5,6],[7,7]]
输出:false
hint:
  • 2 <= coordinates.length <= 1000
  • coordinates[i].length == 2
  • -10^4 <= coordinates[i][0], coordinates[i][1] <= 10^4
  • No duplicate points in coordinates

Guess you like

Origin blog.csdn.net/l1606468155/article/details/103981846