Given n points on a 2D plane, find the maximum number of points that lie on the same straight line

思路:最多的点,必然是点连成线时,所有斜率相同的最多的组合情况;

     那么如果不在同一直线点的组合也可能斜率相同,找其中一点与其它点连即可。

#include <iostream>
#include <vector>
#include <map> using namespace std; struct Point {
int x;
int y;
Point(): x(), y() {}
Point(int a, int b): x(a), y(b) {}
}; class Solution {
public:
int maxPoints(vector<Point> &points) {
if (points.size() < )
return points.size(); int result = ;
for (int i = ; i < points.size(); i++) {
map<pair<int, int>, int> line; int overlap = , vertical = , curMax = ;
for (int j = i + ; j < points.size(); j++) {
if((points[i].x == points[j].x) &&
(points[i].y == points[j].y)) {
overlap ++;
continue;
}
else if (points[i].x == points[j].x) {
vertical ++;
}
else {
int dx = points[i].x - points[j].x;
int dy = points[i].y - points[j].y; int gcd = GCD(dx, dy); dx /= gcd;
dy /= gcd; line[make_pair(dx, dy)]++;
curMax = max(line[make_pair(dx, dy)], curMax);
}
curMax = max(vertical, curMax);
}
result = max(result, curMax+overlap+);
}
return result;
} int GCD(int a, int b) {
if (b == )
return a;
return GCD(b, a%b);
}
}; int main() {
vector<Point> points; points.push_back(Point(, ));
points.push_back(Point(, ));
points.push_back(Point(, ));
Solution *solution = new Solution();
cout << solution->maxPoints(points) << endl; // (3,10),(0,2),(0,2),(3,10)
vector<Point> points2;
points2.push_back(Point(, ));
points2.push_back(Point(, ));
points2.push_back(Point(, ));
points2.push_back(Point(, ));
cout << solution->maxPoints(points2) << endl; return ;
}

leetcode-[3]Max Points on a Line的更多相关文章

  1. 【leetcode】Max Points on a Line

    Max Points on a Line 题目描述: Given n points on a 2D plane, find the maximum number of points that lie ...

  2. [LeetCode OJ] Max Points on a Line

    Max Points on a Line Submission Details 27 / 27 test cases passed. Status: Accepted Runtime: 472 ms ...

  3. [leetcode]149. Max Points on a Line多点共线

    Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. ...

  4. [LeetCode] 149. Max Points on a Line 共线点个数

    Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. ...

  5. 【leetcode】Max Points on a Line(hard)☆

    Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. ...

  6. Java for LeetCode 149 Max Points on a Line

    Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. ...

  7. leetcode 149. Max Points on a Line --------- java

    Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. ...

  8. [LeetCode OJ] Max Points on a Line—Given n points on a 2D plane, find the maximum number of points that lie on the same straight line.

    //定义二维平面上的点struct Point { int x; int y; Point(, ):x(a),y(b){} }; bool operator==(const Point& le ...

  9. LeetCode之Max Points on a Line Total

    1.问题描述 Given n points on a 2D plane, find the maximum number of points that lie on the same straight ...

  10. leetcode[149]Max Points on a Line

    Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. ...

随机推荐

  1. 796B Find The Bone

    B. Find The Bone time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...

  2. javascript 高级程序设计 八

    function 类型: 1.ECMAscript中函数和类C语言的函数有这很多不同.其中之一就是js的函数没有重载.并且多次定义一个同名的函数,当调用这个函数的时候, 会调用最后一次定义的函数. 2 ...

  3. is not allowed to connect to this MySQL server解决办法

    GRANT ALL PRIVILEGES ON *.* TO 'myuser'@'%' IDENTIFIED BY 'mypassword' WITH GRANT OPTION; myuser:代表你 ...

  4. solr搜索配置权重

    配置权重 <requestHandler name="/browse" class="solr.SearchHandler" default=" ...

  5. Web应用获取文件路径的方法

    拥有 HttpServletRequest req 对象 req.getSession().getServletContext().getRealPath("/")   ----- ...

  6. BS架构如何实现即时消息提醒

    转载地址:https://blog.csdn.net/cyjch/article/details/51506434

  7. Git使用基础篇(zz)

    Git使用基础篇 您的评价:          收藏该经验       Git是一个分布式的版本控制工具,本篇文章从介绍Git开始,重点在于介绍Git的基本命令和使用技巧,让你尝试使用Git的同时,体 ...

  8. 【搜索】 Prime Path

    #include<cstdio> #include<cstring> #include<cmath> #include<queue> #include& ...

  9. GO介绍,环境的配置和安装 简单使用

    1. 介绍与安装 Golang 是什么 Go 亦称为 Golang(按照 Rob Pike 说法,语言叫做 Go,Golang 只是官方网站的网址),是由谷歌开发的一个开源的编译型的静态语言. Gol ...

  10. 解决IDEA、Pycharm连接数据库乱码的问题

    一.IDEA. 使用IDEA连接数据库: import java.sql.Connection;import java.sql.DriverManager;import java.sql.Result ...