max-points-on-a-line
来源:互联网 发布:淘宝考试题目答案 编辑:程序博客网 时间:2024/06/10 00:05
题目描述
Given n points on a 2D plane, find the maximum number of points that lie on the same straight line.
IDEA
求出在一条直线上的最大点数。
直线的可能情况:1.在一条垂直x轴的直线上(斜率无穷大),2.斜率相同
CODE
/** * 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 size=points.size(); if(size==0||size==1) return size; int res=1; for(int i=0;i<size;i++){ map<double,int> m;//m->first斜率 m->second点数 int curmax=1; int dup=0;//与当前点重合的点数 int vcnt=0;//与当前点在一条垂直线上的,即横坐标相同, for(int j=0;j<size;j++){ if(j!=i){ double x1=points[i].x-points[j].x; double y1=points[i].y-points[j].y; if(x1==0&&y1==0){ dup++; }else if(x1==0){ if(vcnt==0){ vcnt=2; }else{ vcnt++; } curmax=max(curmax,vcnt); }else{ double k=y1/x1; if(m[k]==0){ m[k]=2; }else{ m[k]++; } curmax=max(curmax,m[k]); } } } res=max(res,curmax+dup); } return res; }};
0 0
- Max Points on a Line
- Max Points on a Line
- Max Points on a Line
- Max Points on a Line
- Max Points on a Line
- Max Points on a Line
- Max Points On a Line
- Max Points on a Line
- Max Points on a Line
- Max Points on a Line
- max points on a line
- Max Points on a Line
- Max Points on a Line
- Max Points on a Line
- Max Points on a Line
- Max Points on a Line
- Max Points on a Line
- Max Points on a Line
- 56. Merge Intervals\113. Path Sum II\211. Add and Search Word
- ubuntu11.10 sources.list 资源
- cpu的工作原理及程序调用过程中栈的变化
- 面试题4:替换空格
- netlink---Linux下基于socket的内核和上层通信机制(上)
- max-points-on-a-line
- Spring事务管理(详解+实例)
- DS18B20简单解析
- java -Math常用方法
- Styling Broken Images (给加载失败的图片加上css样式)
- 【安装mongoose模块报错】解决方案
- android studio 导出jar
- Matrix图片变换处理
- 圆形图片绘制