• 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.

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

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

     1 /**
     2  * Definition for a point.
     3  * struct Point {
     4  *     int x;
     5  *     int y;
     6  *     Point() : x(0), y(0) {}
     7  *     Point(int a, int b) : x(a), y(b) {}
     8  * };
     9  */
    10 class Solution {
    11 public:
    12     int maxPoints(vector<Point> &points) {
    13         int len = points.size();
    14         map<float,int> slopes;
    15         int res=0;
    16         for(int i=0;i<len;i++){
    17             slopes.clear();
    18             int duplicate=1;
    19             for(int j=i+1;j<len;j++){
    20                 if(points[i].x==points[j].x&&points[i].y==points[j].y){
    21                     duplicate++;
    22                 }
    23                 else{
    24                     float temp= points[i].x==points[j].x? INT_MAX :(float)(points[i].y-points[j].y)/(points[i].x-points[j].x);
    25                     slopes[temp]++;
    26                 }
    27                 
    28             }
    29             res=max(res,duplicate);
    30             for(auto pair:slopes){
    31                 res=max(res,pair.second+duplicate);
    32             }
    33         }
    34         return res;
    35     }
    36 };
  • 相关阅读:
    Mysql 备份 导入导出
    简 历
    Mysql 表结构 创建 限制 关联
    Unity 碰撞检测
    Unity 获取键值
    关于大型网站系统的一些问题
    关于zookeeper
    dubbo分布式和消息队列
    集群
    cookie及安全问题
  • 原文地址:https://www.cnblogs.com/zl1991/p/6952995.html
Copyright © 2020-2023  润新知