题意

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

意思就是说在给定的节点中计算出在同一条直线上的最大节点个数。

思路

这道题,题意很容易理解,但是需要注意的东西包括,如果你用斜率计算,那么就需要注意到精确度的问题,否则就会变成相等的斜率,无奈之下,只能提高精确度,比如说用long double,但这不是持久的办法,目前的办法是使用最大公约数。

其实我大概的思路已经想到了,就是第五种方法的思路,这道题没做出来的原因是,没有考虑到相同点以及相同的x的情况。

代码

#include <stdio.h>
#include <stdio.h>
#include <string>
#include <vector>
#include <iostream>
#include <unordered_map>
#include <map>
#include <set>
using namespace std;/*** Definition for a point.*/
struct Point {int x;int y;Point() : x(0), y(0) {}Point(int a, int b) : x(a), y(b) {}
};/***  第一种方法:将每一个顶点和其他的顶点进行计算得出斜率,*  用hash保存下来,key为斜率,value为总个数*  因为相同斜率的则在同一条线上**  @param points <#points description#>**  @return <#return value description#>*/
int maxPoints(vector<Point>& points) {unordered_map<int, int> hash;int maxCnt = 0;auto caculateK = [&](Point p1, Point p2) {int k = (p2.y - p1.y)/(p2.x - p1.x);return k;};for (int i = 0; i < points.size(); i++) {Point pointA = points[i];for (int j = 0; j < points.size() && i != j; j++) {Point pointB = points[j];int k = caculateK(pointA, pointB);if (hash.find(k) != hash.end()) {hash[k]++;}else {hash.insert(make_pair(k, 1));}}//        hash.clear();}for (auto itr = hash.begin(); itr != hash.end(); itr++) {maxCnt = std::max((*itr).second, maxCnt);}return maxCnt + 2;
}/***  计算最大公约数*/
int GCD(int a, int b) {if(b==0) return a;else return GCD(b, a%b);
}/***  这个方法不再是使用斜率作为map的key,而是使用的是gcd(最大公约数)*  总之结果就是overlap+localmax+vertical+1,*  其中overlap代表的是完全相同的顶点,vertical代表的是只有x是相等的,因为相减会导致0**  @param points <#points description#>**  @return <#return value description#>*/
int maxPoints2(vector<Point>& points) {if (points.size() < 2) {return (int)points.size();}int result = 0;for (int i = 0; i < points.size(); i++) {map<pair<int, int>, int> lines;int localmax = 0, overlap = 0, vertical = 0;for (int j = i+1; j < points.size(); j++) {// 相同的点的个数if (points[j].x == points[i].x && points[j].y == points[i].y) {overlap++;continue;}else if (points[j].x == points[i].x) {vertical++;}else {int a=points[j].x-points[i].x, b=points[j].y-points[i].y;int gcd=GCD(a, b);a/=gcd;b/=gcd;lines[make_pair(a, b)]++;localmax=std::max(lines[make_pair(a, b)], localmax);}localmax=std::max(vertical, localmax);}// 结果为result = std::max(result, localmax+overlap+1); //这里加一是因为之前vertical只计算了一次}return result;
}// 由于精确度问题,更改double
inline bool double_equal(double a, double b) { return abs(a-b) < 1e-10; }
inline bool double_less (double a, double b) { return a-b < -1e-10; }struct Line {double r;  // ratio ; slopedouble t;  // translationLine(Point p, Point q) { // mathif (q.x == p.x) r = 1e20, t = p.x;else{r = (double) (q.y-p.y) / (double) (q.x-p.x);t = p.y - p.x * r;}}
};// 用作排序
bool operator < (const Line& a, const Line& b) {return a.r == b.r ? a.t < b.t : a.r < b.r;
}// 依此来判断map中是否相等
bool operator == (const Line& a, const Line& b) {return a.r == b.r && a.t == b.t;
}/***  这个思路最大的特点就是利用了一个结构体和set的原理*  因为set可以自动去除掉重复的元素**  @param points <#points description#>**  @return <#return value description#>*/
int maxPoints4(vector<Point> &points) {if (points.empty()) return 0;map<Line, set<Point*> > line_map;for (auto & a : points)for (auto & b : points){Line line(a,b);line_map[line].insert(&a);line_map[line].insert(&b);}int ret = 1;for (auto & pr : line_map) ret = max(ret,(int)pr.second.size());return ret;
}/***  最直接最简单的做法**  @param points <#points description#>**  @return <#return value description#>*/
int maxPoints5(vector<Point>& points) {if(points.empty())return 0;else if(points.size() == 1)return 1;int ret = 0;for(int i = 0; i < points.size(); i ++){//start pointint curmax = 1; //points[i] itselfunordered_map<long double, int> kcnt;    // slope_k countint vcnt = 0;   // vertical countint dup = 0;    // duplicate added to curmaxfor(int j = 0; j < points.size(); j ++){if(j != i){long double deltax = points[i].x - points[j].x;long double deltay = points[i].y - points[j].y;if(deltax == 0 && deltay == 0) {dup ++;}else if(deltax == 0){if(vcnt == 0)vcnt = 2;elsevcnt ++;curmax = max(curmax, vcnt);}else{long double k = deltay / deltax;if(kcnt[k] == 0)kcnt[k] = 2;elsekcnt[k] ++;curmax = max(curmax, kcnt[k]);}}}ret = max(ret, curmax + dup);}return ret;
}int main(int argc, const char * argv[]) {vector<Point> points;Point point1(0, 0), point2(94911151, 94911150), point3(94911152, 94911151), point4(5, 6);points.push_back(point1);points.push_back(point2);points.push_back(point3);int result = maxPoints5(points);cout << "result..." << result << endl;return 0;
}

转载于:https://www.cnblogs.com/George1994/p/6376531.html

[LeetCode] Max Points on a Line 题解相关推荐

  1. LeetCode: Max Points on a Line

    LeetCode: Max Points on a Line LeetCode: Max Points on a Line Given n points on a 2D plane, find the ...

  2. 解题报告: LeetCode Max Points on a Line

    题目出处:https://leetcode.com/submissions/detail/47640173/ 题意描述: 由于过于简洁,故不再翻译,具体描述见一下英文描述: Given n point ...

  3. LeetCode 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] 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 149. Max Points on a Line

    LeetCode 149. Max Points on a Line Solution1: 参考花花酱:https://zxi.mytechroad.com/blog/geometry/leetcod ...

  6. 【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 ...

  7. leetcode 149. Max Points on a Line |149. 直线上最多的点数(Java)

    题目 https://leetcode.com/problems/max-points-on-a-line/ 题解 hard 题,普通解法不难,有几个小坑: key : key : value 的存储 ...

  8. 【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. ...

  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 ...

最新文章

  1. 分布式TCP压力测试工具 tcpcopy
  2. seq2seq编码器和解码器:TensorFlow实现
  3. createDocumentFragment 详解
  4. Java黑皮书课后题第10章:*10.26(计算器)修改程序清单7-9,接收一个字符串表达式,其中操作符和操作数由0到多个空格隔开
  5. php java session共享_PHP实现session共享
  6. SAP Netweaver ECATT介绍
  7. php框架之laravel
  8. mongo-express 远程代码执行漏洞(CVE-2019-10758)
  9. Node.js 应用故障排查手册 —— 类死循环导致进程阻塞
  10. c语言非法字符空格,98行的四则计算器.(支持括号)加入了非法字符的检测
  11. 贪吃蛇大作战游戏攻略
  12. DOM操作之确定元素大小
  13. Excel数据透视表中的值计算
  14. C++二维vector初始化
  15. AI开发者大会:2020年7月3日和7月4日CSDN重磅举办《百万人学AI》大会议程及其对应视频回放链接
  16. html颜色代码错误,HTML颜色代码表
  17. 互联网行业公司岗位与发展方向
  18. 当免费模式遭遇安全价值观
  19. 解决android studio打包后安装APK提示“签名不一致,该应用可能已被修改。“
  20. APP提现之微信服务号红包

热门文章

  1. hihoCoder #1639 图书馆
  2. C#基础--类/接口/成员修饰符,多态、重载、重写,静态和非静态
  3. Codeigniter 获取当前的控制器名称和方法名称
  4. [BZOJ1177][Apio2009]Oil
  5. iOS之数组的排序(升序、降序及乱序)
  6. 关于sql中去换行符的问题
  7. python web 框架(八)-- Scrapy
  8. Day 1 二分搜索训练总结
  9. js中的preventDefault
  10. 带进度的文件复制 - 回复 冷风无泪 的问题