Given n points on a 2D plane, find the maximum number of points that lie on the same straight line.

Have you met this question in a real interview?

 
 
Example

Given 4 points: (1,2), (3,6), (0,0), (1,3).

The maximum number is 3.

LeetCode上的原题,请参见我之前的博客Max Points on a Line

解法一:

class Solution {
public:
/**
* @param points an array of point
* @return an integer
*/
int maxPoints(vector<Point>& points) {
int res = , n = points.size();
for (int i = ; i < n; ++i) {
int duplicate = ;
unordered_map<double, int> m;
for (int j = i + ; j < n; ++j) {
if (points[i].x == points[j].x && points[i].y == points[j].y) {
++duplicate;
} else if (points[i].x == points[j].x) {
++m[INT_MAX];
} else {
double slope = (double)(points[j].y - points[i].y) / (points[j].x - points[i].x);
++m[slope];
}
}
res = max(res, duplicate);
for (auto it : m) {
res = max(res, it.second + duplicate);
}
}
return res;
}
};

解法二:

class Solution {
public:
/**
* @param points an array of point
* @return an integer
*/
int maxPoints(vector<Point>& points) {
int res = , n = points.size();
for (int i = ; i < n; ++i) {
int duplicate = ;
map<pair<int, int>, int> m;
for (int j = i + ; j < n; ++j) {
if (points[i].x == points[j].x && points[i].y == points[j].y) {
++duplicate; continue;
}
int dx = points[j].x - points[i].x;
int dy = points[j].y - points[i].y;
int d = gcd(dx, dy);
++m[{dx / d, dy / d}];
}
res = max(res, duplicate);
for (auto it : m) {
res = max(res, it.second + duplicate);
}
}
return res;
}
int gcd(int a, int b) {
return (b == ) ? a : gcd(b, a % b);
}
};

解法三:

class Solution {
public:
/**
* @param points an array of point
* @return an integer
*/
int maxPoints(vector<Point>& points) {
int res = ;
for (int i = ; i < points.size(); ++i) {
int repeat = ;
for (int j = i + ; j < points.size(); ++j) {
int cnt = ;
int x1 = points[i].x, y1 = points[i].y;
int x2 = points[j].x, y2 = points[j].y;
if (x1 == x2 && y1 == y2) {++repeat; continue;}
for (int k = ; k < points.size(); ++k) {
int x3 = points[k].x, y3 = points[k].y;
if (x1 * y2 + x2 * y3 + x3 * y1 - x3 * y2 - x2 * y1 - x1 * y3 == ) {
++cnt;
}
}
res = max(res, cnt);
}
res = max(res, repeat);
}
return points.empty() ? : res;
}
};

[LintCode] Max Points on a Line 共线点个数的更多相关文章

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

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

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

  4. [LeetCode OJ] Max Points on a Line

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

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

  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 on the ...

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

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

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

随机推荐

  1. AutoRegister ASM AOP 字节码 案例 原理 MD

    Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...

  2. zeromq学习笔记2——简单的客户端和服务端测试程序

    1.前言 zeromq提供了guide,http://zguide.zeromq.org/,可以帮助新手快速上手,提供了C\C++\PHP等多种语言. 2.测试程序 使用zeromq给的hwserve ...

  3. 使用SpringBoot配置了 server.servlet.path后无效的解决方案

    一.问题描述 使用SpringBoot配置了 server.servlet.path后无效,访问时无法通过:http://127.0.0.1:8080/app/hello.html 访问. 二.解决方 ...

  4. Java字节码 小结

    Reference javap 基本使用方法 深入理解java字节码 从Java代码到字节码 Java字节码.class文件案例分析 字节码 核心概念 Class文件是8位字节流,按字节对齐.之所以称 ...

  5. SpringBoot 2.x 整合ElasticSearch的demo

    SpringBoot 2.x 整合ElasticSearch的demo 1.配置文件application.yml信息 # Tomcat server: tomcat: uri-encoding: U ...

  6. windows 系统中的 afd 驱动

    afd 的全称是 Ancillary Function Driver for WinSock,是 windows 系统网络部分的核心工具.同 Linux 类似,windows 的 socket 最终也 ...

  7. fiddler展示serverIP方法

    转载自:http://www.xuanfengge.com/fiddler-displays-the-set-ip-method.html 前言 由于web前端在多个环境中开发,需要经常更换host, ...

  8. ios NSURLSession后台传输

    http://www.appcoda.com/background-transfer-service-ios7/ http://www.raywenderlich.com/51127/nsurlses ...

  9. node.js中的框架

    node.js中的框架 载自: http://nodeframework.com/ MVC frameworks Sinatra-like These frameworks offer rich co ...

  10. 服务端怎样暴露IBinder接口对象

    服务端怎样暴露IBinder接口对象: package com.example.mydownload; import android.app.Service; import android.conte ...