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的更多相关文章

  1. 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 ...

  2. [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. ...

  3. [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 ...

  4. [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 ...

  5. 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 ...

  6. 【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 ...

  7. [LeetCode OJ] Max Points on a Line

    Max Points on a Line Submission Details 27 / 27 test cases passed. Status: Accepted Runtime: 472 ms ...

  8. 【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 ...

  9. [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. ...

  10. [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. ...

随机推荐

  1. Android开发:《Gradle Recipes for Android》阅读笔记(翻译)2.7——使用Android Studio签署发布apk

    问题: 想要使用Android studio生成签名配置,给他们分配build类型. 解决方案: Build菜单提供了生成签名配置,Project Structure窗口有tab用于分配不同的type ...

  2. 【BZOJ1977】[BeiJing2010组队]次小生成树 Tree 最小生成树+倍增

    [BZOJ1977][BeiJing2010组队]次小生成树 Tree Description 小 C 最近学了很多最小生成树的算法,Prim 算法.Kurskal 算法.消圈算法等等. 正当小 C ...

  3. Frosh Week(归并排序求逆序数)

    H - Frosh Week Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Desc ...

  4. html5 上传头像示例及其注意事项

    转自[B5教程网]:http://www.bcty365.com/content-142-5244-1.html 这次分享一个简易的上传头像示例,其大致流程为: 一.将选择的图片转为base64字符串 ...

  5. LOJ#2230. 「BJOI2014」大融合

    LOJ#2230. 「BJOI2014」大融合 题目描述 小强要在$N$个孤立的星球上建立起一套通信系统.这套通信系统就是连接$N$个点的一个树.这个树的边是一条一条添加上去的. 在某个时刻,一条边的 ...

  6. MySQL中的SQL流程分析简述

    分析MySQL中这条语句的整个流程 update table_a set c1=xx where c2=xxx 朋友考我的一个问题在此处列出个人见解 1 客户端连接进来首先进行权限验证 2 验证通过后 ...

  7. Python3.6全栈开发实例[013]

    13.用户输入的信息,如果出现了列表中的敏感词汇,请用*替代. li = ["苍老师","东京热","武藤兰","波多野结衣&qu ...

  8. Oracle学习笔记—oracle体系架构及状态(nomount、mount和open)简介

    oracle体系架构简介 先来简要了解一下Oracle数据库体系架构以便于后面深入理解,Oracle Server主要由实例(instance)和数据库(database)组成.实例(instance ...

  9. 联想Y50用U盘改装win7的详细教程

    由于一些原因,部分网友想把自带的win8.1系统改成win7,苦于Y50没有光驱,装系统不方便,下面特意做一个用U盘改装系统的教程,先准备一个8G或更大的U盘,如果里面有重要文件,请先备份,等会要清空 ...

  10. java定时器的使用(Timer)(转发:https://blog.csdn.net/ecjtuxuan/article/details/2093757)

    1.在应用开发中,经常需要一些周期性的操作,比如每5分钟执行某一操作等. 对于这样的操作最方便.高效的实现方式就是使用java.util.Timer工具类. private java.util.Tim ...