标签:color i++ str com nbsp contains out loading info
问题:
给定一组坐标点,问这些坐标点是否在一条直线上。
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
标签:color i++ str com nbsp contains out loading info
原文地址:https://www.cnblogs.com/habibah-chang/p/13335047.html