1232. Check If It Is a Straight Line
Posted habibah-chang
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了1232. Check If It Is a Straight Line相关的知识,希望对你有一定的参考价值。
问题:
给定一组坐标点,问这些坐标点是否在一条直线上。
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.
解法:
任意两点之间点斜率slope(k)相等。
double slope=(y1-y2)/(x1-x2)
?? 注意:
特殊的两条线,平行于x轴,y轴的两种线
平行于x轴的斜率为0,
平行于y轴的斜率为∞,这里假设为INT_MAX
代码参考:
1 class Solution { 2 public: 3 bool checkStraightLine(vector<vector<int>>& coordinates) { 4 int N=coordinates.size(); 5 if(N==2) return true; 6 double k=(coordinates[1][0]-coordinates[0][0]==0)?INT_MAX:(coordinates[1][1]-coordinates[0][1])*1.0/(coordinates[1][0]-coordinates[0][0]); 7 for(int i=2; i<N; i++){ 8 double k1=(coordinates[i][0]-coordinates[i-1][0]==0)?INT_MAX:(coordinates[i][1]-coordinates[i-1][1])*1.0/(coordinates[i][0]-coordinates[i-1][0]); 9 if(k1!=k) return false; 10 } 11 return true; 12 } 13 };
以上是关于1232. Check If It Is a Straight Line的主要内容,如果未能解决你的问题,请参考以下文章
LeetCode --- 1232. Check If It Is a Straight Line 解题报告
php WooCommerce助手功能 - 作者:https://faish.al/2014/01/06/check-if-it-is-woocommerce-page/
为啥改不了hosts文件 please check whether if this file is opened in another
Check if a configuration profile is installed on iOS
Check if a string is NULL or EMPTY using PowerShell
LeetCode 1150. Check If a Number Is Majority Element in a Sorted Array