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

找出二维平面上处于同一条直线上的最大点数

PS: map用法。for(auto pair:slopes)pair.second

 /**
* 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) {
int len = points.size();
map<float,int> slopes;
int res=;
for(int i=;i<len;i++){
slopes.clear();
int duplicate=;
for(int j=i+;j<len;j++){
if(points[i].x==points[j].x&&points[i].y==points[j].y){
duplicate++;
}
else{
float temp= points[i].x==points[j].x? INT_MAX :(float)(points[i].y-points[j].y)/(points[i].x-points[j].x);
slopes[temp]++;
} }
res=max(res,duplicate);
for(auto pair:slopes){
res=max(res,pair.second+duplicate);
}
}
return res;
}
};

max-points-on-a-line——穷举的更多相关文章

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

  2. [LeetCode OJ] Max Points on a Line

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

  3. [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. ...

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

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

  6. [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. ...

  7. Max Points on a Line leetcode java

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

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

  9. 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 lin ...

  10. [LeetCode] 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. ...

随机推荐

  1. One-to-one

    创建模型 在本例中,Place 和 Restaurant 为一对一关系 from django.db import models class Place(models.Model): name = m ...

  2. LeetCode(3)Longest Substring Without Repeating Characters

    题目: Given a string, find the length of the longest substring without repeating characters. For examp ...

  3. set的应用:UVa10815-Andy's First Dictionary

    Andy's First Dictionary Andy, 8, has a dream - he wants to produce his very own dictionary. This is ...

  4. 算法学习记录-图——最小生成树之Kruskal算法

    之前的Prim算法是基于顶点查找的算法,而Kruskal则是从边入手. 通俗的讲:就是希望通过 边的权值大小 来寻找最小生成树.(所有的边称为边集合,最小生成树形成的过程中的顶点集合称为W) 选取边集 ...

  5. 高性能MySQL(第三版)

    一.MySQL架构与历史 1.2.2 锁粒度 表锁:写锁的优先级高于读锁:写锁的请求可以插入到读锁的前面,但读锁的请求却不能插入到写锁的前面: 行级锁:行级锁只在存储引擎层实现,在服务器层没有实现: ...

  6. 大数据学习——Storm学习单词计数案例

    需求:计算单词在文档中出现的次数,每出现一次就累加一次 遇到的问题 这个问题是<scope>provided</scope>作用域问题 https://www.cnblogs. ...

  7. 九度oj 题目1114:神奇的口袋

    题目描述: 有一个神奇的口袋,总的容积是40,用这个口袋可以变出一些物品,这些物品的总体积必须是40.John现在有n个想要得到的物品,每个物品的体积分别是a1,a2……an.John可以从这些物品中 ...

  8. linux移动硬盘unmount报错处理

    备份数据用的移动硬盘unmount时报: 通过fuser命令来kill掉设备进程,再unmount移动设备 先看fuser命令帮助信息: [root@athenadb2 ~]# fuser -help ...

  9. 【bzoj1174】[Balkan2007]Toponyms Trie树

    题目描述 给你一个字符集合,你从其中找出一些字符串出来. 希望你找出来的这些字符串的最长公共前缀*字符串的总个数最大化. 输入 第一行给出数字N.N在[2,1000000] 下面N行描述这些字符串,长 ...

  10. BZOJ 4810 [Ynoi2017]由乃的玉米田 ——Bitset 莫队算法

    加法和减法的操作都能想到Bitset. 然后发现乘法比较难办,反正复杂度已经是$O(n\log{n})$了 枚举因数也不能更差了,直接枚举就好了. #include <map> #incl ...