题目描述

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

我的想法:

1、算法设计很简单的题目,但是真正用数据结构实现起来却不容易的一道题。

2、刚开始考虑分三类进行解决,一类横坐标相同,一类纵坐标相同的点,一类斜率点

3、考虑不周,点的坐标可正可负,还想着用数组下标保存,有点蠢

4、思路半路夭折,看了别人的思路。

int maxPoints(vector<Point> &points) {int n = points.size();if (n == 0) return 0;int maxNum = INT_MIN;map<int, vector<Point>> k;set<int> kindK;int shu[10005] = {0}; //shu[i]表示纵坐标相同是i的点的个数int heng[10005] = {0}; //heng[i]表示横坐标相同是i的点的个数int maxX = INT_MIN, maxY = INT_MIN;int minX = INT_MAX, minY = INT_MAX;for (int i = 0; i < n; i++) {for (int j = 0; j < n; j++) {int ix = points[i].x, iy = points[i].y;maxX = max(ix, maxX);minX = min(ix, minX);maxY = max(maxY, iy);minY = min(iy,minY);int jx = points[j].x, jy = points[j].y;maxX = max(jx, maxX);minX = min(jx, minX);maxY = max(maxY, jy);minY = min(jy,minY);if (ix == jx) {  //在一条竖线上,横坐标相同heng[ix]++;} else if (iy == jy) {shu[iy]++;} else {int xie = (jy - iy) / (jx - ix);kindK.insert(xie);vector<Point> tmp;tmp = k[xie];tmp.push_back(points[i]);tmp.push_back(points[j]);k[xie] = tmp;}}}for(int i = minX; i <= maxX; i++){if(heng[i] == 0) continue;else{maxNum = max(maxNum, heng[i]);}}for(int i = minY; i <= maxY; i++){if(shu[i] == 0) continue;else{maxNum = max(maxNum, shu[i]);}}for(auto x : kindK){vector<Point> tmp = k[x];int b[10005] = {0};int maxB = INT_MIN;for(int i = 0; i < tmp.size(); i++){int bb = tmp[i].y - x*tmp[i].x;b[bb]++;maxB = max(bb,maxB);}for(int i = 0; i <= maxB; i++)}}

牛客网友的思路:

1.分两层循环枚举,第一遍遍历起始点a,第二遍遍历剩下的点

2.分两类,垂直和有斜率的,要注意计算垂直点不为0时,不能重复计算

链接:https://www.nowcoder.com/questionTerminal/bfc691e0100441cdb8ec153f32540be2?f=discussion
来源:牛客网/*** 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) {int size = points.size();if(size == 0)return 0;else if(size == 1)return 1;int ret = 0;for(int i = 0;i<size;i++){int curmax = 1;map<double,int>mp;int vcnt = 0; //垂直点int dup = 0; //重复点for(int j = 0;j<size;j++){if(j!=i){double x1 = points[i].x - points[j].x;double y1 = points[i].y - points[j].y;if(x1 == 0 && y1 == 0){   //重复dup++;}else if(x1 == 0){      //垂直if(vcnt == 0)vcnt = 2;elsevcnt++;curmax = max(vcnt,curmax);}else{double k = y1/x1;          //斜率if(mp[k] == 0)mp[k] = 2;elsemp[k]++;curmax = max(mp[k],curmax);}                   }}ret = max(ret,curmax+dup);           }return ret;}
};

牛客 -- 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 lin ...

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

  6. 【重要+细节】LeetCode 149. Max Points on a Line

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

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

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

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

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

最新文章

  1. linux定时导出数据库,linux 下定时导出备份oracle数据库
  2. 五大关键物联网应用助力优化数据中心基础设施管理
  3. DM8168 系统编译、启动、烧写
  4. 肝了十几个小时的java反射,希望对大家有所帮助吧!
  5. Android Studio git 版本回退到最新的版本
  6. SequoiaDB 系列之五 :源码分析之main函数
  7. OpenExpressApp对建模支持的初步计划
  8. linux-basic(7)linux文件与目录管理
  9. ocp linux 基础要点
  10. mysql日志(介绍 路径修改 备份)
  11. JBPM工作流入门总结
  12. leetcode lcp2 分式化简
  13. Node.js的完全卸载与下载安装及各种npm、nvm、nrm配置(保姆式教程---提供全套安装包)---node.js的安装与配置(0)
  14. java math.floordiv,Math类的常用方法--田小江
  15. 三星Note3开发者模式
  16. REST Assured 1 - REST Assured 介绍
  17. java JPG等图片格式转成PGM
  18. mybatis的大于小于号转义符号
  19. mysql启动报错之[ERROR] Found option without preceding group in config file /etc/my.cnf at line
  20. C语言校验 checksum

热门文章

  1. 2_7 FlyweightMode 享元模式
  2. Android 图形系统
  3. LevelDB 源码剖析(六)WAL模块:LOG 结构、读写流程、崩溃恢复
  4. 为什么我建议你现在学Vue3?
  5. 【今晚七点半】:对话平行未来姜雨晴——重启熊猫直播背后的故事
  6. 音视频技术开发周刊 | 170
  7. LiveVideoStackCon 2020北京站 | 参会指南
  8. 第一届WebRTCon在上海举行
  9. ​Linux CPU 性能优化指南
  10. 教育机构如何提升在线教育技术能力? | 云+社区技术沙龙