# 1232. 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.

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
``````

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) {
int x1 = coordinates[0][0], y1 = coordinates[0][1];
int x2 = coordinates[1][0], y2 = coordinates[1][1];
for (int i = 2; i < coordinates.size(); ++i) {
int x3 = coordinates[i][0], y3 = coordinates[i][1];
if ((x2 - x1) * (y3 - y1) != (y2 - y1) * (x3 - x1)) return false;
}
return true;
}
};
``````

Github 同步地址:

https://github.com/grandyang/leetcode/issues/1232

https://leetcode.com/problems/check-if-it-is-a-straight-line/

https://leetcode.com/problems/check-if-it-is-a-straight-line/discuss/408984/JavaPython-3-check-slopes-short-code-w-explanation-and-analysis.

https://leetcode.com/problems/check-if-it-is-a-straight-line/discuss/620096/Java-Python3-CPP-or-Simple-code-with-explanation-or-100-fast-or-O(1)-space

LeetCode All in One 题目讲解汇总(持续更新中…)

|

Venmo 打赏

—|—

 微信打赏 Venmo 打赏
（欢迎加入博主的知识星球，博主将及时答疑解惑，并分享刷题经验与总结，试运营期间前五十位可享受半价优惠～）

×

Help us with donation