[LeetCode] 149. Max Points on a Line
来源:互联网 发布:淘宝搜索重定向效果 编辑:程序博客网 时间:2024/04/26 06:52
思路:
对于每个点, 都建造一个哈希表, key是其他剩下的点跟该点组成的直线的斜率, value是该斜率上的点的记数. 但要记得需要额外的变量记录重复点的个数和垂直于x轴的直线上的点的个数. 这些要特殊记录的.
int maxPoints(vector<Point>& points) { unordered_map<double, int> m; int res = 0; for (int i = 0; i < points.size(); i++) { int dup = 1; int vertical = 1; for (int j = i + 1; j < points.size(); j++) { Point p1 = points[i]; Point p2 = points[j]; if (p1.x == p2.x) { if (p1.y == p2.y) dup++; else vertical++; } else { double slope = (p2.y - p1.y) * 1.0 / (p2.x - p1.x); m[slope]++; } } int cur = 0; for (pair<double, int> p : m) cur = max(cur, p.second); res = max(res, cur + dup); res = max(res, vertical); m.clear(); } return res;}
0 0
- LeetCode 149. Max Points on a Line
- [leetcode] 149. Max Points on a Line
- leetcode 149. Max Points on a Line
- Leetcode 149. Max Points on a Line
- LeetCode 149. Max Points on a Line
- leetcode.149. Max Points on a Line
- LeetCode-149.Max Points on a Line
- leetcode 149. Max Points on a Line
- leetCode 149. Max Points on a Line
- [LeetCode] 149. Max Points on a Line
- LeetCode 149. Max Points on a Line
- [LeetCode]149. Max Points on a Line
- [LeetCode]149. Max Points on a Line
- [LeetCode]149. Max Points on a Line
- Leetcode 149. Max Points on a Line
- [LeetCode] 149. Max Points on a Line
- Leetcode 149. Max Points on a Line
- 【LeetCode】149. Max Points on a Line
- Ubuntu16.04换源
- HDU5443——The Water Problem(2015年长春区域赛)
- android 获取相册列表的实现及源码下载(四)
- junit参数化测试
- 在一个由升序序列旋转之后的序列中查找最小元素
- [LeetCode] 149. Max Points on a Line
- 读书随笔总结
- 获取mysql数据表中自动生成的主键c
- Android Support Design库之TextInputLayout
- BZOJ 1190: [HNOI2007]梦幻岛宝珠
- IO流——字节流——字符流
- centos 安装jenkins
- Java基础_猴子分桃问题
- do {...} while (0) 在宏定义中的作用