/**
* Definition for a point.
* struct Point {
* int x;
* int y;
* Point() : x(0), y(0) {}
* Point(int a, int b) : x(a), y(b) {}
* };
*/
class Solution {
public:
int maxPoints(vector<Point>& points) {
if (points.size() <= 2)
{
return points.size();
}
set<double> find;
set<double> findx;
set<double> findy;
int max = 0;
for (int i = 0; i < points.size() - 1; i++)
{
for (int j = i + 1; j < points.size(); j++)
{
if (points[j].x != points[i].x && points[j].y != points[i].y)
{
double d = (double)(points[j].x - points[i].x) / (points[j].y - points[i].y);
if (find.find(d)!=find.end())
{
continue;
}
find.insert(d);
int num = 1;
for (int z = 0; z < points.size(); z++)
{
if (z != i)
{
double d1 = (double)(points[z].x - points[i].x) / (points[z].y - points[i].y);
if (d == d1 || (points[z].x == points[i].x && points[z].y == points[i].y))
{
num++;
}
}
}
if (num > max)
{
max = num;
}
}
else if (points[j].y != points[i].y)
{
double d = points[j].x;
if (findx.find(d) != findx.end())
{
continue;
}
findx.insert(d);
int num = 1;
for (int z = 0; z < points.size(); z++)
{
if (z != i)
{
if (points[z].x == points[i].x)
{
num++;
}
}
}
if (num > max)
{
max = num;
}
}
else if (points[j].x != points[i].x)
{
double d = points[j].y;
if (findy.find(d) != findy.end())
{
continue;
}
findy.insert(d);
int num = 1;
for (int z = 0; z < points.size(); z++)
{
if (z != i)
{
if (points[z].y == points[i].y)
{
num++;
}
}
}
if (num > max)
{
max = num;
}
}
else
{
continue;
}
}
}
if (max == 0)
{
return points.size();
}
return max;
}
};
* Definition for a point.
* struct Point {
* int x;
* int y;
* Point() : x(0), y(0) {}
* Point(int a, int b) : x(a), y(b) {}
* };
*/
class Solution {
public:
int maxPoints(vector<Point>& points) {
if (points.size() <= 2)
{
return points.size();
}
set<double> find;
set<double> findx;
set<double> findy;
int max = 0;
for (int i = 0; i < points.size() - 1; i++)
{
for (int j = i + 1; j < points.size(); j++)
{
if (points[j].x != points[i].x && points[j].y != points[i].y)
{
double d = (double)(points[j].x - points[i].x) / (points[j].y - points[i].y);
if (find.find(d)!=find.end())
{
continue;
}
find.insert(d);
int num = 1;
for (int z = 0; z < points.size(); z++)
{
if (z != i)
{
double d1 = (double)(points[z].x - points[i].x) / (points[z].y - points[i].y);
if (d == d1 || (points[z].x == points[i].x && points[z].y == points[i].y))
{
num++;
}
}
}
if (num > max)
{
max = num;
}
}
else if (points[j].y != points[i].y)
{
double d = points[j].x;
if (findx.find(d) != findx.end())
{
continue;
}
findx.insert(d);
int num = 1;
for (int z = 0; z < points.size(); z++)
{
if (z != i)
{
if (points[z].x == points[i].x)
{
num++;
}
}
}
if (num > max)
{
max = num;
}
}
else if (points[j].x != points[i].x)
{
double d = points[j].y;
if (findy.find(d) != findy.end())
{
continue;
}
findy.insert(d);
int num = 1;
for (int z = 0; z < points.size(); z++)
{
if (z != i)
{
if (points[z].y == points[i].y)
{
num++;
}
}
}
if (num > max)
{
max = num;
}
}
else
{
continue;
}
}
}
if (max == 0)
{
return points.size();
}
return max;
}
};
评论
发表评论