leetcode之Max Points on a Line
来源:互联网 发布:北京电影学院动画 知乎 编辑:程序博客网 时间:2024/05/01 16:40
问题描述如下:
Given n points on a 2D plane, find the maximum number of points that lie on the same straight line.
问题链接
cpp代码如下:
class Solution {public: int maxPoints(vector<Point> &points) { int size_=points.size(); if(size_==0)return 0; int ans=0; for(int i=0;i<size_;++i){ map<float,int> cnts; int ver_cnt=1; int same=0; for(int j=i+1;j<size_;++j){ int x=points[i].x-points[j].x; int y=points[i].y-points[j].y; if(x==0&&y==0){ ++same; ver_cnt++; continue; } if(x==0){ ver_cnt++; continue; } float k=((float)y)/x; if(cnts.count(k)==0) cnts[k]=2; else cnts[k]++; } int tmp=ver_cnt; for(map<float,int>::iterator p=cnts.begin();p!=cnts.end();++p) if(p->second+same>tmp) tmp=p->second+same; if(tmp>ans)ans=tmp; } return ans; }};
0 0
- LeetCode 之 Max Points on a Line
- leetcode之Max Points on a Line
- Leetcode之 Max Points on a Line
- leetcode之Max Points on a Line
- LeetCode之Max Points on a Line
- Leetcode之三Max Points on a Line
- Max Points on a Line | leetcode
- Max Points on a Line LEETCODE
- Leetcode: Max Points on a Line
- LeetCode - Max Points on a Line
- LeetCode | Max Points on a Line
- [LeetCode] Max Points on a Line
- Leetcode: Max Points on a Line
- LeetCode OJ:Max Points on a Line
- Leetcode Max Points on a Line
- Leetcode: Max Points on a Line .
- [Leetcode] Max Points on a Line (Java)
- leetcode:【Max Points on a Line】
- 第十五周 项目一--打入“内部”寻“内幕”
- 第十五周oj--指针的基本操作3
- 第15周项目3(4)-在OJ上玩指针
- PHP二维数组去重
- Eclipse中设置作者日期等信息
- leetcode之Max Points on a Line
- 背景图片太大时,而又不出现竖向滚动条的做法
- 第十五周项目三OJ题目(2)
- mysql 开启慢查询
- 用指针比较两数的大小(2)
- centos7 安装ipvsadm
- svn 64bit 不显示图标
- 第十五周 项目3(4)两数和与差
- 三数之和为0的三指针实现