leetCode 149. Max Points on a Line
来源:互联网 发布:linux查看mysql库 编辑:程序博客网 时间:2024/04/24 00:19
很简单,看线段的斜率就可以了。
/** * 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) { if(points.empty())return 0; else if(points.size()==1)return 1; int res = 0; for(int i=0;i<points.size();i++){ int curmax = 1; unordered_map<double,int>kcnt; int vcnt = 0; int dup = 0; for(int j=0;j<points.size();j++){ if(j!=i){ double deltax = points[i].x-points[j].x; double deltay = points[i].y-points[j].y; if(deltax==0&&deltay==0)dup++; else if(deltax==0){ if(vcnt==0) vcnt+=2; else vcnt++; curmax =max(curmax,vcnt); } else { double k = deltay/deltax; if(kcnt[k]==0) kcnt[k]=2; else kcnt[k]++; curmax = max(curmax,kcnt[k]); } } } res = max(res,dup+curmax); } 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
- 设计模式之桥梁模式--- Pattern Bridge
- SPOJ PHRASESRelevant Phrases of Annihilation
- iOS开发把数组按照条件分组
- 测试一个带图带代码的内容文章
- FreeMarker基础配置
- leetCode 149. Max Points on a Line
- C语言学习12
- learn word2vector from scratch -- part 3
- Python基础字符串知识点小结
- iOS本地通知——每天固定时间发送通知
- 【剑指offer】反转链表并返回链表的头指针
- 数组中和等于K的数对
- 最后一公里极速配送(三)
- 线段树区间和最大值