LeetCode : Max Points on a Line
来源:互联网 发布:验厂app软件 编辑:程序博客网 时间:2024/06/06 07:28
Given n points on a 2D plane, find the maximum number of points that lie on the same straight line.
int maxPoints(vector<Point> &points) {int length=points.size();int maxpoints=0;if(length<1) return 0;if(length==1) return 1;double *Slope=new double[length-1];multimap<double ,double> mp;multimap<double ,double> mpTemp;for (int j = 0; j < length; j++){Point p=points[j];double x0=p.x,y0=p.y;double x1=0,y1=0;double k=0;//斜率int equalNum=0;multimap<double,double >::iterator iter;multimap<double,double >::iterator iterTemp;multimap<double,double >::iterator iter1;for (int i = 0; i < length; i++){x1=points[i].x;y1=points[i].y;if((x1-x0)!=0){k=(y1-y0)/(x1-x0);}else if((x1-x0)==0&&(y1-y0)!=0){k=1.79769e+308;}else if((x1-x0)==0&&(y1-y0)==0){equalNum++;continue;}iter=mp.find(k);if(iter==mp.end()){mp.insert(pair<double,double >(k,2));}else{iter->second+=1;}}if(equalNum>1){for(iter=mp.begin();iter!=mp.end();++iter){iter->second+=1;}}for (iterTemp=mp.begin(); iterTemp!=mp.end(); iterTemp++){mpTemp.insert(pair<double,double>(iterTemp->second,iterTemp->first));}iter1=mpTemp.end();if(mpTemp.size()>0){iter1--;if(iter1->first>maxpoints){maxpoints=iter1->first;}}else{maxpoints=equalNum;}mpTemp.clear();mp.clear();}return maxpoints; }
0 0
- Max Points on a Line | leetcode
- Max Points on a Line LEETCODE
- Leetcode: Max Points on a Line
- LeetCode - Max Points on a Line
- LeetCode 之 Max Points on a Line
- LeetCode | Max Points on a Line
- [LeetCode] Max Points on a Line
- Leetcode: Max Points on a Line
- LeetCode OJ:Max Points on a Line
- Leetcode Max Points on a Line
- Leetcode: Max Points on a Line .
- [Leetcode] Max Points on a Line (Java)
- leetcode:【Max Points on a Line】
- Max Points on a Line - LeetCode
- LeetCode:Max Points on a Line
- [LeetCode] Max Points on a Line
- leetcode - Max Points on a Line
- LeetCode(149)Max Points on a Line
- ww的笔记之FMDB操作类
- 51系列小型操作系统精髓 简单实现7 C语言版待改进
- 初识makefile
- 如何实现android程序的多屏适配
- 25个增强iOS应用程序性能的提示和技巧--中级篇
- LeetCode : Max Points on a Line
- Robotframework / RIDE Win7安装步骤
- Android上9patch对话框制作与使用
- Android 4.2 Wifi Display 之 Settings 源码分析(一)
- 25个增强iOS应用程序性能的提示和技巧--高级篇
- vs2012远程调试(可用)
- python各种模块的使用
- Android 4.2 Wifi Display 之 Settings 源码分析(二)
- 使用ant编译成功但返回ERRORLEVEL为1的小错误