【LintCode】Max Points on a Line(笔记)
来源:互联网 发布:网络棋牌平台排名2017 编辑:程序博客网 时间:2024/06/16 08:12
Description
Given n points on a 2D plane, find the maximum number of points that lie on the same straight line.
(二维平面上有n个点,求出共线的最多点数)
Example
Given 4 points: (1,2)
, (3,6)
, (0,0)
, (1,3)
.
The maximum number is 3
.
本想的方法是用斜率来计算,每两个点间计算斜率,因为有些细节没注意到总不能通过。
只给出了1个点或2个点的情况;
有的点重复给出的情况(对其就不要计算斜率了,把重复次数记录下来);
斜率不能计算的情况(在一条竖线上,slope可以用 INT_MAX 来表示);
在一条横线上的情况(其实和一般斜率的情况一样计算,结果为0而已);
Solution
固定一个点,计算一轮其他点和该点的斜率(用map计数,斜率相同的点就在同一直线上),找到该点固定下的最多点数max。
计算斜率时,要注意【重复的点】和【斜率为无穷大即x值相同的点】。
/** * 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: /** * @param points an array of point * @return an integer */ int maxPoints(vector<Point>& points) { // Write your code here if(points.size()<3) return points.size(); int max = 0; map<double, int> slopeMapNum; for(int fix=0; fix<points.size(); fix++){ //依次作为固定点 Point fixP = points[fix]; slopeMapNum.erase(slopeMapNum.begin(), slopeMapNum.end()); int cover = 1; for(int i=0; i<points.size(); i++){ double slope; if(i==fix) //同一个点,跳过 continue; if(fixP.x==points[i].x && fixP.y==points[i].y){ //与固定点重复的点 cover++; continue; } else if(fixP.x==points[i].x){ //与固定点在同一条竖线上 slope = INT_MAX; } else{ slope = 1.0*(fixP.y-points[i].y)/(fixP.x-points[i].x); } map<double, int>::iterator it; it = slopeMapNum.find(slope); if(it==slopeMapNum.end()) slopeMapNum.insert(pair<double,int>(slope,1)); else slopeMapNum[slope] += 1; } if(slopeMapNum.empty()) max = cover>max ? cover:max; else{ map<double, int>::iterator j; for(j=slopeMapNum.begin(); j!=slopeMapNum.end(); j++) max = (j->second+cover)>max ? (j->second+cover):max; } } return max; }};
C++ Notes
// map插入元素myMap["a"] = 1; myMap.insert(map<string, int>::value_type("b",2)); myMap.insert(pair<string, int>("c",3)); myMap.insert(make_pair<string, int>("d",4));// 删除map某键的元素cmap.erase("bfff"); map<string,int>::iterator it = cmap.find("mykey");if(it!=cmap.end()) cmap.erase(it);// 删除所有元素cmap.erase(cmap.begin(),cmap.end()); // 迭代mapmap<string, int>::iterator it;for(it=myMap.begin(); it!=myMap.end(); ++it){}// 其他成员方法myMap.empty()myMap.size()myMap.clear() //清空
阅读全文
0 0
- 【LintCode】Max Points on a Line(笔记)
- Lintcode - Max points on a line
- lintcode Max Points on a Line
- **[Lintcode] Max Points on a Line
- (leetcode)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
- 基于jenkins搭建一个分布式持续集成服务器
- LA 4998 Simple Encryption (数论 dfs)
- iOS应用状态保存和恢复
- CentOS_6.5配置iptables防火墙策略
- 从业务骨干到管理精英
- 【LintCode】Max Points on a Line(笔记)
- android应用的视频压缩
- 不关闭seLinux解决vsftpd服务本地用户不能登录问题(500 OOPS: cannot change directory:/home/***)
- 查询MySql数据库物理文件存放位置
- 安卓开发实现长连接,心跳,java后端,实现消息推送,持续更新中
- Saltstack生产案例讲解
- cmd 里面运行git提示“不是内部或外部命令,也不是可运行的程序”的解决办法
- Android代码优化
- Spark的Master、Worker、Executor以及task等概念