leetcode 149. Max Points on a Line 计算斜率的问题 + 直接暴力求解即可

﹏ヽ暗。殇╰゛Y 2022-06-08 06:24 254阅读 0赞

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

本题就是统计在一条线上的最多的点的数量。

解决方法就是遍历求解,但是主语Java的浮点数的计算精度的问题。

这道题就必须要考虑精度的问题,否者部分case是过不去的,

代码如下:

  1. import java.math.BigDecimal;
  2. import java.util.HashMap;
  3. /*class Point
  4. {
  5. int x;
  6. int y;
  7. Point() { x = 0; y = 0; }
  8. Point(int a, int b) { x = a; y = b; }
  9. }*/
  10. /*
  11. * 注意Java的浮点数精度
  12. * */
  13. public class Solution
  14. {
  15. public int maxPoints(Point[] points)
  16. {
  17. if (points.length <= 2)
  18. return points.length;
  19. int max = 2;
  20. for (int i = 0; i < points.length; i++)
  21. {
  22. int pointMax = 1, samePointCount = 0;
  23. HashMap<Double, Integer> slopeCount = new HashMap<Double, Integer>();
  24. for (int j = i + 1; j < points.length; j++)
  25. {
  26. if (points[i].x == points[j].x && points[i].y == points[j].y)
  27. {
  28. samePointCount++;
  29. continue;
  30. }
  31. double k;
  32. if (points[i].x == points[j].x)
  33. k = Float.POSITIVE_INFINITY;
  34. //这里是为了避免出现-0.0和0.0的问题
  35. else if (points[i].y == points[j].y)
  36. k = 0.0;
  37. else
  38. {
  39. //这里是为了避免出现(k,k+1),(k+1,k+2)和(0,0)的斜率精度问题
  40. //这里是考虑到了Java的Double的精度问题,所以才是用BigDecimal的问题
  41. BigDecimal fenziBigDecimal=new BigDecimal(points[i].x-points[j].x);
  42. BigDecimal fenmuBigDecimal=new BigDecimal(points[i].y-points[j].y);
  43. k = fenziBigDecimal.divide(fenmuBigDecimal,20,BigDecimal.ROUND_HALF_DOWN).doubleValue();
  44. }
  45. slopeCount.put(k, slopeCount.getOrDefault(k, 1)+1);
  46. pointMax = Math.max(pointMax, slopeCount.get(k));
  47. }
  48. max = Math.max(max, pointMax+samePointCount);
  49. }
  50. return max;
  51. }
  52. }

下面是C++的做法,就是遍历所有的可能性,注意可能出现极端情况,所以这里使用了long double 来计算极端情况的斜率值

pointMax设置为1是为了处理所有的点全部相同的情况,别的都好处理

代码如下:

  1. #include <iostream>
  2. #include <climits>
  3. #include <map>
  4. #include <vector>
  5. #include <algorithm>
  6. using namespace std;
  7. /*
  8. struct Point
  9. {
  10. int x;
  11. int y;
  12. Point() : x(0), y(0) {}
  13. Point(int a, int b) : x(a), y(b) {}
  14. };
  15. */
  16. class Solution
  17. {
  18. public:
  19. int maxPoints(vector<Point>& points)
  20. {
  21. if (points.size() <= 2)
  22. return points.size();
  23. int maxRes = 2;
  24. for (int i = 0; i < points.size(); i++)
  25. {
  26. int pointMax = 1, samePoint = 0;
  27. map<long double, int> count;
  28. for (int j = i + 1; j < points.size(); j++)
  29. {
  30. if (points[i].x == points[j].x && points[i].y == points[j].y)
  31. {
  32. samePoint++;
  33. continue;
  34. }
  35. long double k = 0;
  36. if (points[i].x == points[j].x)
  37. k = numeric_limits<double>::max();
  38. else
  39. k = ((long double)points[i].y - (long double)points[j].y) / ((long double)points[i].x - (long double)points[j].x);
  40. if (count.find(k) == count.end())
  41. count[k] = 2;
  42. else
  43. count[k] += 1;
  44. pointMax = max(pointMax,count[k]);
  45. }
  46. maxRes = max(maxRes, pointMax+samePoint);
  47. }
  48. return maxRes;
  49. }
  50. };

发表评论

表情:
评论列表 (有 0 条评论,254人围观)

还没有评论,来说两句吧...

相关阅读