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

【题意】

求二维平面上n个点中,最多共线的点数。

转自:http://blog.csdn.net/doc_sgl/article/details/17103427

这道题思想很简单··············

分析:

任意一条直线都可以表述为

y = ax + b

假设,有两个点(x1,y1), (x2,y2),如果他们都在这条直线上则有

y1 = kx1 +b

y2 = kx2 +b

由此可以得到关系,k = (y2-y1)/(x2-x1)。即如果点c和点a的斜率为k, 而点b和点a的斜率也为k,可以知道点c和点b也在一条线上。

取定一个点points[i], 遍历其他所有节点, 然后统计斜率相同的点数,并求取最大值即可。

计算斜率时,注意重合点和x值相同的两个点(数学上称斜率不存在,此时斜率用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:
int maxPoints(vector<Point>& points) {
unordered_map<float,int> mp;
int maxNum = ;
for(int i = ; i < points.size(); i++)
{
mp.clear();
mp[INT_MIN] = ;
int duplicate = ;
for(int j = ; j < points.size(); j++)
{
if(j == i) continue;
if(points[i].x == points[j].x && points[i].y == points[j].y)
{
duplicate++;
continue;
}
float k = points[i].x == points[j].x ? INT_MAX : (float)(points[j].y - points[i].y)/(points[j].x - points[i].x);
mp[k]++;
}
unordered_map<float, int>::iterator it = mp.begin();
for(; it != mp.end(); it++)
if(it->second + duplicate > maxNum)
maxNum = it->second + duplicate;
}
return maxNum; }
};

注意:

0、points中重复出现的点。

1、int maxNum = 0;

初始化,以防points.size() ==0的情况。

2、mp[INT_MIN] = 0;

保证poins中只有一个结点,还有points中只有重复元素时,mp中没有元素。这两种极端情况。

3、int duplicate = 1;

duplicate记录重复点的数量,初始化为1,是因为要把当前的点points[i]加进去。

4、float k = points[i].x == points[j].x ? INT_MAX : (float)(points[j].y - points[i].y)/(points[j].x - points[i].x);

计算斜率,如果直线和y轴平行,就取INT_MAX,否则就取(float)(points[j].y - points[i].y)/(points[j].x - points[i].x)

一开始把(float)(points[j].y - points[i].y)/(points[j].x - points[i].x)写做(float)((points[j].y - points[i].y)/(points[j].x - points[i].x))一直就不对,后来才想明白,注意注意!

Max Points on a Line——数学&&Map的更多相关文章

  1. [LeetCode OJ] Max Points on a Line

    Max Points on a Line Submission Details 27 / 27 test cases passed. Status: Accepted Runtime: 472 ms ...

  2. [LintCode] 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. ...

  3. 【LeetCode】149. 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 on the ...

  4. 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 on the ...

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

  6. 149. Max Points on a Line

    题目: Given n points on a 2D plane, find the maximum number of points that lie on the same straight li ...

  7. [leetcode]149. 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. ...

  8. Max Points on a Line (HASH TABLE

    QUESTIONGiven n points on a 2D plane, find the maximum number of points that lie on the same straigh ...

  9. 【Max Points on a Line 】cpp

    题目: Given n points on a 2D plane, find the maximum number of points that lie on the same straight li ...

随机推荐

  1. 转ajax的jsonp的文章

    转:http://justcoding.iteye.com/blog/1366102/ Js是不能跨域请求.出于安全考虑,js设计时不可以跨域. 什么是跨域: 1.域名不同时. 2.域名相同,端口不同 ...

  2. [linux/net]策略路由实现特定主机特定路径

        echo 200 silence >> /etc/iproute2/rt_tables ip rule add from 10.192.0.230 table silence ip ...

  3. bzoj 相似回文串 3350 3103 弦图染色+manacher

    相似回文串 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 143  Solved: 68[Submit][Status][Discuss] Descr ...

  4. FreeRTOS - 中断使用注意

    原文地址:http://www.cnblogs.com/god-of-death/p/6886823.html 注意点: 1.首先要将中断的嵌套全部设置为抢占优先级. 2.将freertos系统内核中 ...

  5. Centos6.5+Python2.7 +ffmpeg+opencv2自动安装脚本

    今天安装opencv折腾了多个小时,为以后安装少走弯路,脚本安装 完整 脚本如下: #! /bin/bash sudo yum install -y gcc g++ gtk+-devel libjpe ...

  6. arm开发板刷机方法

    1.linux系统启动方式 bootloader->kernel->system 在嵌入式系统中内存为DRAM,inand flash 都不能直接启动需要被初始化.其中初始化程序在(boo ...

  7. lombok 配置使用以及优势

    maven依赖 <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> ...

  8. 【BZOJ2946】公共串 [SAM]

    公共串 Time Limit: 3 Sec  Memory Limit: 128 MB[Submit][Status][Discuss] Description 给出几个由小写字母构成的单词,求它们最 ...

  9. 跨域iframe高度计算

    一.同域获取iframe内容 这里有两个细节: 1. 取iframe内的文档对象,标准浏览器使用contentDocument属性,IE低版本(IE6,7,8)使用document属性. 2. cal ...

  10. cuda中的二分查找

    使用背景 通常,在做高性能计算时,我们需要随机的连接某些点.这些点都具有自己的度量值,显然,度量值越大的值随机到的概率就会越大.因此,采用加权值得方法: void getdegreeSum(DG *g ...