LeetCode149:Max Points on a Line
题目:
Given n points on a 2D plane, find the maximum number of points that lie on the same straight line.
解题思路:
1,在所有点中选定一个点作为中心点,然后再求剩下的点到该中心点的斜率,如果斜率相同的点表示在同一直线上
2,如果剩下点中有与中心点相同的点,则记下相同点的个数,然后直接跳过,继续下一个点到中心点斜率的求解
3,为了防止重复计算,当以节点i作为中心节点时,剩余的点表示为数组中i点后面的点
实现代码:
#include <iostream>
#include <vector>
#include <map>
#include <limits>
#include <unordered_map>
using namespace std; /* */
struct Point {
int x;
int y;
Point() : x(), y() {}
Point(int a, int b) : x(a), y(b) {}
}; class Solution {
public:
int maxPoints(vector<Point> &points) {
if(points.size() == )
return ;
int max = ;
map<double, int> umap;
for(int i = ; i < points.size(); i++)
{
int tmp_max = ;//当已第i个点位中心时,同一直线上点数最大值
umap.clear();
int repeat = ;//与i点相同点的个数
for(int j = i+; j < points.size(); j++)
{
double slope = numeric_limits<double>::infinity();
if(points[j].x != points[i].x)
slope = double(points[j].y - points[i].y) / (points[j].x - points[i].x);
else if(points[j].y == points[i].y)//与中心点相同的点
{
repeat++;
continue;
}
umap[slope]++;//到中心点斜率相同的点数++,这里umap中存在该斜率,则直接将该斜率对应的值++,否则先添加,再++
if(umap[slope] > tmp_max)
tmp_max = umap[slope];
}
tmp_max += repeat;//以i为中心点出发的每一条直线上的点数都应该加上repeat,因为与i点相同的点在所有从i出发的直线上
if(tmp_max > max)
max = tmp_max;//更新全局最大值 }
return max + ; //之前所求的每一条直线上的点数都没有加上该直线的中心点,所以这里要加上1
}
}; int main(void)
{
Point ps[] = {{,},{,},{,}};
int len = sizeof(ps) / sizeof(Point);
vector<Point> points(ps, ps+len);
Solution solution;
int ret = solution.maxPoints(points);
cout<<ret<<endl;
return ;
}
LeetCode149:Max Points on a Line的更多相关文章
- 【leetcode】Max Points on a Line
Max Points on a Line 题目描述: Given n points on a 2D plane, find the maximum number of points that lie ...
- [LeetCode OJ] Max Points on a Line
Max Points on a Line Submission Details 27 / 27 test cases passed. Status: Accepted Runtime: 472 ms ...
- [LintCode] Max Points on a Line 共线点个数
Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. ...
- 【LeetCode】149. Max Points on a Line
Max Points on a Line Given n points on a 2D plane, find the maximum number of points that lie on the ...
- LeetCode: Max Points on a Line 解题报告
Max Points on a Line Given n points on a 2D plane, find the maximum number of points that lie on the ...
- [leetcode]149. Max Points on a Line多点共线
Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. ...
- Max Points on a Line leetcode java
题目: Given n points on a 2D plane, find the maximum number of points that lie on the same straight li ...
- 【Max Points on a Line 】cpp
题目: Given n points on a 2D plane, find the maximum number of points that lie on the same straight li ...
- LeetCode(149) Max Points on a Line
题目 Given n points on a 2D plane, find the maximum number of points that lie on the same straight lin ...
随机推荐
- reduction
reduction - 必应词典 美[rɪ'dʌkʃən]英[rɪ'dʌkʃ(ə)n] n.还原:降低:减少:缩小 网络缩减:减量:减小 变形复数:reductions:
- 百度Cafe原理--Android自动化测试学习历程
主要讲解内容及笔记: 一.Cafe原理 Cafe是一款自动化测试框架,解决问题:跨进程测试.快速深度测试 官网:http://baiduqa.github.io/Cafe/ Cafe provides ...
- httplib:AttributeError: 'module' object has no attribute 'HTTPConnection'
# -*-coding:gb2312-*- #Function:学习python的httplib模块 import httplib conn = httplib.HTTPConnection(&quo ...
- [leetcode]680. Valid Palindrome II有效回文II(可至多删一原字符)
Given a non-empty string s, you may delete at most one character. Judge whether you can make it a pa ...
- PAT 1047 编程团体赛(代码)
1047 编程团体赛(20)(20 分) 编程团体赛的规则为:每个参赛队由若干队员组成:所有队员独立比赛:参赛队的成绩为所有队员的成绩和:成绩最高的队获胜. 现给定所有队员的比赛成绩,请你编写程序找出 ...
- 201621123008 《Java程序设计》第一周学习总结
1. 本章学习总结 对于我们学计算机的学生而言,要想提高编程能力,只有多练习,把我们所学到的东西运用到实践中去,整天抱着书本冥思苦想而不动手到具体的环境中去试验是很难有所提升的.大一学C语言的时候平时 ...
- Codeforces 679B. Barnicle 模拟
B. Barnicle time limit per test: 1 second memory limit per test :256 megabytes input: standard input ...
- 旅游类APP原型模板分享——爱彼迎
这是一款专为游客提供全球范围内短租服务的APP,可以让你不论出门在外或在家附近都能开展探索之旅,并且还可以获取世界各地独特房源.当地体验及好去处等相关信息. 这款APP层级清晰简明,此原型模板所用到的 ...
- 淘宝星星的思路c#
<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head><meta ...
- VS快捷键以及Reshaper快捷键
VS快捷键: Resharper 快捷键(此图是保存他人的[具体是谁忘记了]): 参考: http://msdn.microsoft.com/zh-cn/library/da5kh0wa.aspx