题目链接: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;
}
};

Lint Code——最多共线的点的个数的更多相关文章

  1. 牛客:t次询问,每次给你一个数n,求在[1,n]内约数个数最多的数的约数个数(数论+贪心)

    https://ac.nowcoder.com/acm/contest/907/B t次询问,每次给你一个数n,求在[1,n]内约数个数最多的数的约数个数 分析: 根据约数和定理:对于一个大于1正整数 ...

  2. luogu 1142 轰炸 最多共线点数

    题目链接 题意 给定\(n(n\leq 700)\)个点,问共线的点最多有多少个? 思路 \(O(n^3)\):枚举两个顶点确定一条直线,再看有多少个顶点在这条直线上.讲道理会T. \(O(n^2lo ...

  3. 查找String中出现最多字符的次数和个数

    Sting 的charAt方法返回相应位置的字符,使用该方法遍历String,将每个字符存入对象属性,遍历属性得到最多字符个数 <!DOCTYPE html> <html> & ...

  4. UVA - 294 Divisors【数论/区间内约数最多的数的约数个数】

    Mathematicians love all sorts of odd properties of numbers. For instance, they consider to be an int ...

  5. sonar Lint ----code bad smell

    类名注释报黄: 去掉这段黄做法:alt+enter 本文参考: http://www.cnblogs.com/xxoome/p/6677170.html

  6. Lint Code 1365. Minimum Cycle Section

    这题可以看作POJ 1961 最小循环节的一个简化版本.某补习班广告贴里给出的两个指针的参考解法简直大误. 受POJ 1961的启发,把数组看作字串,观察可知,如果字串全部由循环节构成(包括最后一段是 ...

  7. Code+ A 晨跑【三个数的最小公倍数】

    时间限制:C/C++ 1秒,其他语言2秒空间限制:C/C++ 262144K,其他语言524288K64bit IO Format: %lld 题目描述 “无体育,不清华”.“每天锻炼一小时,健康工作 ...

  8. Visual Studio Code 配置指南

    Visual Studio Code (简称 VS Code)是由微软研发的一款免费.开源的跨平台文本(代码)编辑器.在我看来它是「一款完美的编辑器」. 本文是有关 VS Code 的特性介绍与配置指 ...

  9. nowcoder N约数个数

    n的约数个数 题目:t次询问,每次给你一个数n,求在[1,n]内约数个数最多的数的约数个数 数据:对于100%的数据,t <= 500 , 1 <= n <= 10000000000 ...

随机推荐

  1. events.js:72 throw er; // Unhandled 'error' event

    http://stackoverflow.com/questions/24525253/events-js72-throw-er-unhandled-error-event 出现了一个上述的错误. e ...

  2. Thrift源码解析--transport

    这一层主要是用于实现网络通信,现在都是基于Tcp/Ip,而Tcp/Ip协议栈由socket来实现,换句话说就是现在网络通信服务底层大都是通过socket实现的,在thrift源码中,就是将socket ...

  3. HDU1577-WisKey的眼神

    Problem Description WisKey的眼镜有500多度,所以眼神不大好,而且他有个习惯,就是走路喜欢看着地(不是为了拣钱哦^_^),所以大家下次碰见他的时候最好主动打下招呼,呵呵.但是 ...

  4. CSS3秘笈:第四章

    第四章  继承 1.继承:应用在一个标签上的CSS样式被传到其内嵌标签上的过程. 2.继承的局限性: (1)有些属性不会被继承,如:CSS. (2)以下情况不会严格执行继承: ·影响网页元素位置的属性 ...

  5. 远程连接(ssh安装)

    更新源列表打开"终端窗口",输入"sudo apt-get update"-->回车-->"输入当前登录用户的管理员密码"--& ...

  6. 如何清除jboss缓存

    要清除Jboss下的缓存,只要清除以下文件的所有文件就可以了:1.D:\JavaServer\jboss-4.2.2.GA\server\default\tmp2.D:\JavaServer\jbos ...

  7. js1中call和apply的用法

    js1中call和apply的用法 е辊顷 饼蹭瑭 岚辗疥 碜坪命 笛攮鼠 鲳篝等 ざ遛膜 镀鞭冢蒯 晕 册薷濑 就不是抓了而是人拳啪啪两声两个人都被拳头打在了腿骨 许郾犍 国 ...

  8. Oracle where 0=1 or 1=1

    本文转载自:http://www.cnblogs.com/junyuz/archive/2011/03/10/1979646.html sql where 1=1和 0=1 的作用   where 1 ...

  9. TOMCAT-publishing to tomcat v7.0 server at

    因为tomcat的work文件没有清空,导致MyEclipse部署在server.xml文件中的项目路径是错误的. 解决办法:清空work文件夹 下面这个勾勾是MyEclipse自动发布项目路径的选项

  10. 转 [分享一个SQL] 查会话阻塞关系,层次关系.

    with ash as (select /*+ materialize*/* from DBA_HIST_ACTIVE_SESS_HISTORY  where sample_time between ...