题目描述:
给你一个数组 points
,其中 points[i] = [xi, yi]
表示 X-Y 平面上的一个点。求最多有多少个点在同一条直线上。
示例 1:
输入:points = [[1,1],[2,2],[3,3]] 输出:3
示例 2:
输入:points = [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]] 输出:4
题源:https://leetcode-cn.com/problems/max-points-on-a-line/
题解:感觉就是数学题
class Solution { public: int maxPoints(vector<vector<int>>& points) { int l=points.size(); int res=1,ans=0; bool flag[305]; memset(flag,0,sizeof(flag)); for(int i=0;i<l;i++) { memset(flag,0,sizeof(flag)); flag[i]=1; for(int j=i+1;j<l;j++) { //double k=(points[j][1]-points[i][1])*1.0/(points[j][0]-points[i][0]); ans=2; flag[j]=1; for(int t=j+1;t<l;t++) { if (!flag[t]) { // double kk=(points[t][1]-points[i][1])*1.0/(points[t][0]-points[i][0]); if ( ((points[j][1]-points[i][1])*(points[t][0]-points[i][0])) == ((points[t][1]-points[i][1])*(points[j][0]-points[i][0])) ) ans++; } } res=max(res,ans); } } return res; } };