题意

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

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

思路

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

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

代码

  1. #include <stdio.h>
  2. #include <stdio.h>
  3. #include <string>
  4. #include <vector>
  5. #include <iostream>
  6. #include <unordered_map>
  7. #include <map>
  8. #include <set>
  9. using namespace std;
  10. /**
  11. * Definition for a point.
  12. */
  13. struct Point {
  14. int x;
  15. int y;
  16. Point() : x(0), y(0) {}
  17. Point(int a, int b) : x(a), y(b) {}
  18. };
  19. /**
  20. * 第一种方法:将每一个顶点和其他的顶点进行计算得出斜率,
  21. * 用hash保存下来,key为斜率,value为总个数
  22. * 因为相同斜率的则在同一条线上
  23. *
  24. * @param points <#points description#>
  25. *
  26. * @return <#return value description#>
  27. */
  28. int maxPoints(vector<Point>& points) {
  29. unordered_map<int, int> hash;
  30. int maxCnt = 0;
  31. auto caculateK = [&](Point p1, Point p2) {
  32. int k = (p2.y - p1.y)/(p2.x - p1.x);
  33. return k;
  34. };
  35. for (int i = 0; i < points.size(); i++) {
  36. Point pointA = points[i];
  37. for (int j = 0; j < points.size() && i != j; j++) {
  38. Point pointB = points[j];
  39. int k = caculateK(pointA, pointB);
  40. if (hash.find(k) != hash.end()) {
  41. hash[k]++;
  42. }
  43. else {
  44. hash.insert(make_pair(k, 1));
  45. }
  46. }
  47. // hash.clear();
  48. }
  49. for (auto itr = hash.begin(); itr != hash.end(); itr++) {
  50. maxCnt = std::max((*itr).second, maxCnt);
  51. }
  52. return maxCnt + 2;
  53. }
  54. /**
  55. * 计算最大公约数
  56. */
  57. int GCD(int a, int b) {
  58. if(b==0) return a;
  59. else return GCD(b, a%b);
  60. }
  61. /**
  62. * 这个方法不再是使用斜率作为map的key,而是使用的是gcd(最大公约数)
  63. * 总之结果就是overlap+localmax+vertical+1,
  64. * 其中overlap代表的是完全相同的顶点,vertical代表的是只有x是相等的,因为相减会导致0
  65. *
  66. * @param points <#points description#>
  67. *
  68. * @return <#return value description#>
  69. */
  70. int maxPoints2(vector<Point>& points) {
  71. if (points.size() < 2) {
  72. return (int)points.size();
  73. }
  74. int result = 0;
  75. for (int i = 0; i < points.size(); i++) {
  76. map<pair<int, int>, int> lines;
  77. int localmax = 0, overlap = 0, vertical = 0;
  78. for (int j = i+1; j < points.size(); j++) {
  79. // 相同的点的个数
  80. if (points[j].x == points[i].x && points[j].y == points[i].y) {
  81. overlap++;
  82. continue;
  83. }
  84. else if (points[j].x == points[i].x) {
  85. vertical++;
  86. }
  87. else {
  88. int a=points[j].x-points[i].x, b=points[j].y-points[i].y;
  89. int gcd=GCD(a, b);
  90. a/=gcd;
  91. b/=gcd;
  92. lines[make_pair(a, b)]++;
  93. localmax=std::max(lines[make_pair(a, b)], localmax);
  94. }
  95. localmax=std::max(vertical, localmax);
  96. }
  97. // 结果为
  98. result = std::max(result, localmax+overlap+1); //这里加一是因为之前vertical只计算了一次
  99. }
  100. return result;
  101. }
  102. // 由于精确度问题,更改double
  103. inline bool double_equal(double a, double b) { return abs(a-b) < 1e-10; }
  104. inline bool double_less (double a, double b) { return a-b < -1e-10; }
  105. struct Line {
  106. double r; // ratio ; slope
  107. double t; // translation
  108. Line(Point p, Point q) { // math
  109. if (q.x == p.x) r = 1e20, t = p.x;
  110. else
  111. {
  112. r = (double) (q.y-p.y) / (double) (q.x-p.x);
  113. t = p.y - p.x * r;
  114. }
  115. }
  116. };
  117. // 用作排序
  118. bool operator < (const Line& a, const Line& b) {
  119. return a.r == b.r ? a.t < b.t : a.r < b.r;
  120. }
  121. // 依此来判断map中是否相等
  122. bool operator == (const Line& a, const Line& b) {
  123. return a.r == b.r && a.t == b.t;
  124. }
  125. /**
  126. * 这个思路最大的特点就是利用了一个结构体和set的原理
  127. * 因为set可以自动去除掉重复的元素
  128. *
  129. * @param points <#points description#>
  130. *
  131. * @return <#return value description#>
  132. */
  133. int maxPoints4(vector<Point> &points) {
  134. if (points.empty()) return 0;
  135. map<Line, set<Point*> > line_map;
  136. for (auto & a : points)
  137. for (auto & b : points)
  138. {
  139. Line line(a,b);
  140. line_map[line].insert(&a);
  141. line_map[line].insert(&b);
  142. }
  143. int ret = 1;
  144. for (auto & pr : line_map) ret = max(ret,(int)pr.second.size());
  145. return ret;
  146. }
  147. /**
  148. * 最直接最简单的做法
  149. *
  150. * @param points <#points description#>
  151. *
  152. * @return <#return value description#>
  153. */
  154. int maxPoints5(vector<Point>& points) {
  155. if(points.empty())
  156. return 0;
  157. else if(points.size() == 1)
  158. return 1;
  159. int ret = 0;
  160. for(int i = 0; i < points.size(); i ++)
  161. {//start point
  162. int curmax = 1; //points[i] itself
  163. unordered_map<long double, int> kcnt; // slope_k count
  164. int vcnt = 0; // vertical count
  165. int dup = 0; // duplicate added to curmax
  166. for(int j = 0; j < points.size(); j ++)
  167. {
  168. if(j != i)
  169. {
  170. long double deltax = points[i].x - points[j].x;
  171. long double deltay = points[i].y - points[j].y;
  172. if(deltax == 0 && deltay == 0) {
  173. dup ++;
  174. }
  175. else if(deltax == 0)
  176. {
  177. if(vcnt == 0)
  178. vcnt = 2;
  179. else
  180. vcnt ++;
  181. curmax = max(curmax, vcnt);
  182. }
  183. else
  184. {
  185. long double k = deltay / deltax;
  186. if(kcnt[k] == 0)
  187. kcnt[k] = 2;
  188. else
  189. kcnt[k] ++;
  190. curmax = max(curmax, kcnt[k]);
  191. }
  192. }
  193. }
  194. ret = max(ret, curmax + dup);
  195. }
  196. return ret;
  197. }
  198. int main(int argc, const char * argv[]) {
  199. vector<Point> points;
  200. Point point1(0, 0), point2(94911151, 94911150), point3(94911152, 94911151), point4(5, 6);
  201. points.push_back(point1);
  202. points.push_back(point2);
  203. points.push_back(point3);
  204. int result = maxPoints5(points);
  205. cout << "result..." << result << endl;
  206. return 0;
  207. }

[LeetCode] 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 on the ...

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

  3. [leetcode]Max Points on a Line @ Python

    原题地址:https://oj.leetcode.com/problems/max-points-on-a-line/ 题意:Given n points on a 2D plane, find th ...

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

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

  6. [LeetCode OJ] Max Points on a Line

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

  7. 【LeetCode】149. 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 on the ...

  8. [LintCode] 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. ...

  9. Max Points on a Line leetcode java

    题目: Given n points on a 2D plane, find the maximum number of points that lie on the same straight li ...

随机推荐

  1. required_new spring事务传播行为无效碰到的坑!

    在测试事务传播行为的时候,因为用了同一个service中的方法测试,所以不管怎么设置都无效了: 原因是aop动态代理只会拦截一次执行方法,第二个方法是照搬的,只要调用其他service中的事务方法,传 ...

  2. Git管理本地代码(一)【转】

    转自:http://blog.csdn.net/weihan1314/article/details/8677800 版权声明:本文为博主原创文章,未经博主允许不得转载.   目录(?)[+]   安 ...

  3. C 数据结构堆

    引言 - 数据结构堆 堆结构都很耳熟, 从堆排序到优先级队列, 我们总会看见它的身影. 相关的资料太多了, 堆 - https://zh.wikipedia.org/wiki/%E5%A0%86%E7 ...

  4. cocos2d-x 日志...

    cocos2d-x  日志... http://blog.csdn.net/themagickeyjianan/article/details/39008297http://blog.csdn.net ...

  5. 在VirtualBox上安装Ubuntu

    Windows 10家庭中文版,VirtualBox 5.2.12 r122591,Ubuntu ubuntu-18.04-desktop, 前言 很久没用过Linux类的操作系统了,上一次是好多年前 ...

  6. JQuery中DOM事件合成用法

    jQuery有两个合成事件——hover()方法和toggle()方法 类似前面讲过的ready()方法,hover()方法和toggle()方法都属于jQuery自定义的方法. hover()方法: ...

  7. 使用html+css+js实现计算器

    使用html+css+js实现计算器,开启你的计算之旅吧 效果图: 代码如下,复制即可使用: <!DOCTYPE html><html lang="en"> ...

  8. JS中精选this关键字的指向规律你记住了吗

      1.首先要明确:           谁最终调用函数,this指向谁           this指向的永远只可能是对象!!!!!          this指向谁永远不取决于this写在哪,而取 ...

  9. (三)HttpClient 抓取图片

    第一节: HttpClient 抓取图片 这里pom.xml需要用到io输入输出: <dependency> <groupId>commons-io</groupId&g ...

  10. MySql数据库 主从复制/共享 报错

    从 获取不到 共享主的数据, 错误信息: Waiting for master to send event 解决方案: // 1. 从V表获取PrNo的数据 select * from Vendor_ ...