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. MQ对比

    转:http://blog.csdn.net/linsongbin1/article/details/47781187 MQ框架非常之多,比较流行的有RabbitMq.ActiveMq.ZeroMq. ...

  2. HDU1298 字典树+dfs

    T9 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submissi ...

  3. spring boot 在IDEA控制台中打印彩色日志

    只需要在application.properties中加入 spring.output.ansi.enabled=ALWAYS 即可

  4. [Coding Practice] Maximum number of zeros in NxN matrix

    Question: Input is a NxN matrix which contains only 0′s and 1′s. The condition is no 1 will occur in ...

  5. angularJs $resource自定义方法(待完善)

    配置CompanyService var services = angular.module('liaoyuan.services'); services.factory('CompanyServic ...

  6. PHP扩展--Oracle客户端(oci8)安装

    下载Oracle客户端 官方下载地址: Linux X86-64 同意协议,下载以下文件: oracle-instantclient11.2-basic-11.2.0.4.0-1.x86_64.rpm ...

  7. jQuery简单的Ajax调用

    index.php 的代码如下: <!DOCTYPE HTML> <html> <head> <meta charset="UTF-8"& ...

  8. IIS7绑定多个HTTPS网站并应用自签名证书

    本文主要介绍如何在IIS中添加多个网站并使用同一个数字签名证书(win7+IIS7.5) IIS中添加站点site1,端口号为80,主机名为空.如下图: 创建证书 IIS->Server Cer ...

  9. NDK---使用,开发步骤

    使用NDk的场景: 1.某些方法,是使用C,C++本地代码实现的,然后,我想在Java中调用这些方法.这个时候,就需要使用到JNI技术. 应用NDK的时候,分两个部分,Java部分,JNI层部分,本地 ...

  10. bzoj2516 电梯

    传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=2516 [题解] 状压dp. $f_{sta,i}$表示状态为sta,当前在第i层的最小花费时 ...