题目链接: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. Python安装coverage.py

    coverage.py是一个用来统计python程序代码覆盖率的工具.它使用起来非常简单,并且支持最终生成界面友好的html报告.在最新版本中,还提供了分支覆盖的功能. 官方网站: http://ne ...

  2. Servlet、Filter、Listener、Interceptor基础

    第一:Servlet Servlet是个接口,全限定名是javax.servlet.Servlet,在javax.servlet包中,在servlet-api.jar(在tomcat自带的lib文件夹 ...

  3. iOS开发 自定义UIAlertController的样式

    引言: 关于提示框, 系统自带的提示框有时可能满足不了我们的需求, 比如一个提示框的取消按钮我需要灰色字体显示, 这时候就需要自定义提示框的样式了. 示例图 苹果自iOS8开始,就已经废弃了之前用于界 ...

  4. 正则表达式判断ip地址

    html: <div class="configuration"><form action="" name="myformcon&q ...

  5. C++ 使用string一行一行读取文件

    c++ 读取文件中的一行一行数据 通用模板: std::ifstream in(dictpath); if(!in) { std::cout << __DATE__ << &q ...

  6. 《JavaScript高级程序设计》读书笔记 ---if语句

    if语句 大多数编程语言中最为常用的一个语句就是if 语句.以下是if 语句的语法:if (condition) statement1 else statement2其中的condition(条件)可 ...

  7. java操作cookies

    建立一个无生命周期的cookie,即随着浏览器的关闭即消失的cookie,代码如下 HttpServletRequest request HttpServletResponse response Co ...

  8. ios网站

    www.diveinedu.cn/nav/index.html  

  9. docker Swarm 集群发现

    Node 发现 $swarm manage --discovery dockerhost01:,dockerhost02:,dockerhost03: -H= 文件发现 $swarm manage - ...

  10. 【实验室笔记】太阳能板清洁器DEMO

    <太阳能板清洁器DEMO>2015年的毕昇杯比赛作品,用时两天,整体设计思路很简单: [机械结构]: 清洁器主体采用角钢搭建,用钢锯切割好以后,上螺丝,走线用的尼龙扎带捆绑: 清洗滚轮采用 ...