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. ...
随机推荐
- Codeforces Beta Round #25 (Div. 2)--A. IQ test
IQ test time limit per test 2 seconds memory limit per test 256 megabytes input standard input outpu ...
- C#关于AutoResetEvent的使用介绍----修正
说明 之前在博客园看到有位仁兄发表一篇关于AutoResetEvent介绍,看了下他写的代码,看上去没什么问题,但仔细看还是能发现问题.下图是这位仁兄代码截图. 仁兄博客地址:http://www.c ...
- <2013 07 05> 804.15. 4--> TI MSP430+CC2520 调试
这一周,实际参与eCar项目的工作正式展开. 来TUM的第一个月,主要熟悉了eCar的机电结构,特别是熟悉了eCar的IT(Information Technology),包括硬件和代码. 来的时候, ...
- 内置函数:min 用法
内置函数:min 用法 源码 def min(*args, key=None): # known special case of min """ min(iterable ...
- [转载]MySQL concat函数的使用
MySQL concat函数是MySQL数据库中众多的函数之一,下文将对MySQL concat函数的语法和使用进行说明,供您参考和学习. MySQL concat函数使用方法:CONCAT(str1 ...
- github常用的git命令
添加已有项目到github: touch README.md //新建说明文件 git init //在当前项目目录中生成本地git管理,并建立一个隐藏.git目录 git add . //添加当前目 ...
- App的开发过程
不同的项目管理模式或许会有完全不同的流程步骤.但是专业性几乎是保证产品质量的唯一准则. App的开发过程主要分为以下阶段,本文会按顺序为大家简单地说明: 1.需求梳理.分析 2.产品原型图绘制 3.U ...
- Python常见序列详解
一.Python中序列的分类 常见序列类型包括字符串(普通字符串和unicode字符串),列表和元组.所谓序列,即成员有序排列,可通过下标访问. 二.Python序列通用操作 下面我们将分别以字符串. ...
- python学习之路-第一天-接触python
我的入门就决定用<简明Python教程> <简明Python教程> 1. python的优势 简单:专注于解决问题而不是关注语言本身 易学:容易上手 开源.免费 可移植性非常强 ...
- 曾经跳过的坑------replace、替换斜杠反斜杠、时间格式化处理
JAVA 中: 坑一: replace没有用对象进行接收.直接使用 dateStr.replaceAll("\\/", "-"); 是不行的,至少得加上 &qu ...