题目链接:http://www.lintcode.com/zh-cn/problem/max-points-on-a-line/#

条件:给一个点数组

目标:求出共线的点的最多个数

实现:时间复杂度——O(n^2)

   要考虑的特殊情况是:①有相同点(这个也太特喵隐蔽了)②斜率不存在的点

思路:暴力求解,遍历每一个点,与他之后的点进行匹配,用一个map<double,int>存储斜率对应的个数。

代码:

/**
* 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:
/**
* @param points an array of point
* @return an integer
*/
bool check(map<double,int> &res,double k){
map<double ,int >::iterator it;
it=res.find(k);
if(it==res.end()){
return false;
}
return true;
}
void change(map<double,int> &res,double k,int &num){ map<double ,int >::iterator it;
it=res.find(k);
it->second++;
if(it->second>num){
num=it->second;
}
} int maxPoints(vector<Point>& points) {
// Write your code here
int num=;
if(points.size()==){
return num;
}
num=; Point point_i,point_j;
double k; for(int i=;i<points.size();i++){
point_i=points[i];
int same=;
map<double,int> res;
map<double ,int >::iterator it;
for(int j=i+;j<points.size();j++){
point_j=points[j];
if(point_j.x-point_i.x == && point_j.y-point_i.y == ){//同一点
same++;
}else if(point_j.x-point_i.x == && point_j.y-point_i.y !=){
k=(numeric_limits<double>::max)();
if(check(res,k)){
it=res.find(k);
it->second++;
}else {
res.insert(pair<double,int>(k,));
}
}else if(point_j.x-point_i.x != ){
k=(point_j.y-point_i.y)*1.0/(point_j.x-point_i.x);
if(check(res,k)){
it=res.find(k);
it->second++;
}else {
res.insert(pair<double,int>(k,));
}
}
}
if(res.empty()){
if(same>num){
num=same;
}
}
for(it=res.begin();it!=res.end();it++){
if(it->second+same>num){
num=it->second+same;
}
}
}
return num;
}
};

最新文章

  1. 扩大ubuntu虚拟机硬盘空间
  2. ASP.NET Web API 安全筛选器
  3. HDU 3374 最小/大表示法+KMP
  4. Visual C++ 中的重大更改
  5. JS设计模式一:单例模式
  6. I.MX6 SHT20 Linux 驱动移植
  7. jenkins:应用篇(Gatling plugin的使用)
  8. 如何判断TCP包是否发送成功
  9. phper 要求
  10. mydumper工作原理, seconds_behind_master的陷阱和pt-heartbeat (102)
  11. Android 常用系统控件
  12. 3D objects key rendering steps
  13. Maven POM配置释疑
  14. Android: Type Method &#39;NewStringUTF&#39; could not be resolved
  15. USACO Section 1.1-2 Greedy Gift Givers
  16. mysql-高性能索引策略
  17. servlet中 java.lang.ClassNotFoundException: com.mysql.jdbc.Driver异常
  18. 关于H5的一些杂思细想(一)
  19. python文件读和写
  20. 使用Nginx部署静态网站

热门文章

  1. java的string.split()分割特殊字符时注意点
  2. WinHex V18.7(16进制编辑器) 多国语言绿色版
  3. C# 中的局部static变量
  4. 在鼠标右键添加“使用WPS打开”
  5. ssl证书验证
  6. MVC 包命名规范
  7. 抓包工具Fidder详解(主要来抓取Android中app的请求)
  8. WHM使用手册by lin
  9. thinkphp的目录结构设计经验总结1
  10. ice调通过iceReplica用所有server instance的方法---客户端控制服务端的负载均衡