LeetCode——max-points-on-a-line
Question
Given n points on a 2D plane, find the maximum number of points that lie on the same straight line.
Solution
这道题用穷举法求解,时间复杂度为O(n^2)。但是要注意几个细节,如果包含的点数小于等于2,那么直接返回点数。
如果第三个点和前面两个点中的一个相等,那么直接在总数上累加1。如果第三个点和前面两个点的横坐标都一样,那么直接在总数上累加1,如果只和其中一个一样,那么直接计算下一个点。如果两个都不一样,那么就开始计算斜率是否相等,相等的话,总数就累加1,反之。
Code
/**
* 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.size() <= 2)
return points.size();
int maxNumbers = 2;
for (int i = 0; i < points.size(); i++) {
for (int j = i + 1; j < points.size(); j++) {
int count = 2;
for (int k = 0; k < points.size(); k++) {
if (k == i || k == j)
continue;
// 重叠
if ((points[k].x == points[i].x && points[k].y == points[i].y) ||
(points[k].x == points[j].x && points[k].y == points[j].y)) {
count++;
if (count > maxNumbers)
maxNumbers = count;
continue;
}
// 横坐标一样,相减为0,不能计算斜率
if (points[k].x == points[j].x) {
if (points[j].x == points[i].x) {
count++;
if (count > maxNumbers)
maxNumbers = count;
continue;
} else
continue;
} else if (points[j].x == points[i].x) {
continue;
}
// 计算斜率
if ((points[k].y - points[j].y) / (float)(points[k].x - points[j].x) ==
(points[j].y - points[i].y) / (float)(points[j].x - points[i].x))
count++;
if (count > maxNumbers)
maxNumbers = count;
}
}
}
return maxNumbers;
}
};
LeetCode——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 on the ...
- [LeetCode] 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]Max Points on a Line @ Python
原题地址:https://oj.leetcode.com/problems/max-points-on-a-line/ 题意:Given n points on a 2D plane, find th ...
- [LeetCode] 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 ...
- LeetCode:Max Points on a Line
题目链接 Given n points on a 2D plane, find the maximum number of points that lie on the same straight l ...
- 【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 ...
- 【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 ...
- [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多点共线
Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. ...
随机推荐
- linux下装locustio
升级Python版本 #python centOS6.8中默认安装的是2.6版本,因为我在安装到后面的时候报错Python版本较低,所以可以先把python版本升级到2.7: 直接在命令行输入:#wg ...
- CGI FastCGI PHP-CGI PHP-FRM
CGI(Common GateWay Interface )通用网关接口,CGI可以让一个客户端,从网页浏览器向执行在Web服务器上的程序请求数据.CGI描述了客户端和这个程序之间传输数据的一种协议标 ...
- SharePoint服务器端对象模型 之 使用CAML进行数据查询(Part 2)
(三)使用SPQuery进行列表查询 1.概述 列表查询主要是指在一个指定的列表(或文档库)中按照某些筛选.排序条件进行查询.列表查询主要使用SPQuery对象,以及SPList的GetItems方法 ...
- 监听浏览器使用不同版本js并且处理ie兼容getElementByClassName
if(window.addEventListener){ document.write('<script src="js/jquery-2.1.4.min.js">&l ...
- Git中如何将代码恢复到之前某个节点
本文主要讲如何使用小乌龟软件将代码恢复到之前某个节点. 一 说明 在实际项目开发中,都是很多人一起联合开发,往往会遇到这种情况:马上要发版本了,突然发现一个致命BUG,而这个BUG是由于某个小伙伴修改 ...
- 洛谷 P4819 [中山市选]杀人游戏
洛谷 题目就是让我们在DAG中找到一些点,覆盖所有点. 因为是DAG,可以想到tarjan缩一下点.假设我们需要找x个点,那么答案就是(n-x)/n. 我们怎么选点呢? 敏锐的我们很快就能想到,直接选 ...
- Python3 不能直接导入reduce
python 3.0以后, reduce已经不在built-in function里了, 要用它就得from functools import reduce. reduce的用法 reduce(fun ...
- git原理:提交原理
当运行git add git commit的时候,git底层都做了什么? 这里涉及到的底层命令:git hash-object 讲对象写入到git object中git update-index ...
- Centos6.3下Ganglia3.6.0安装配置
近期安装Ganglia.因为之前Linux基础基本为0.因此费了非常大的周折.最后在失败了好多次之后最终看到了梦寐以求的web界面.以下总结下这几天来的工作. ganglia是一个监控软件,他包括三部 ...
- 000-mysql小技巧
1.使用Navicat 链接5.7版本出现 mysql 5.7.9 [Err] 1055报错解决,[Err] 1055 – Expression #1 of ORDER BY clause is no ...