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

Example 1:

Input: [[1,1],[2,2],[3,3]]
Output: 3
Explanation:
^
|
|        o
|     o
|  o  
+------------->
0  1  2  3 4

Example 2:

Input: [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]]
Output: 4
Explanation:
^
|
| o
|     o   o
|      o
|  o   o
+------------------->
0  1  2  3  4  5  6

NOTE: input types have been changed on April 15, 2019. Please reset to default code definition to get new method signature.

Solution:

  直接求每个点与其他点的斜率,然后计算相同斜率的个数就行

  注意两点:

  一个是会有相同的点出现,另一个是斜率计算得到无穷和double比较相等的情况

 class Solution {
public:
int maxPoints(vector<vector<int>>& points) {
if (points.size() < )return points.size();
int res = ;
for (int i = ; i < points.size(); ++i)
{
int same = ;
for (int j = i + ; j < points.size(); ++j)
{
int cnt = ;
long long int x1 = points[i][], x2 = points[j][];
long long int y1 = points[i][], y2 = points[j][];
if (x1 == x2 && y1 == y2) { ++same; continue; }
for (int k = ; k < points.size(); ++k)
{
long long int x3 = points[k][], y3 = points[k][];
if ((x1*y2 + x2 * y3 + x3 * y1 - x3 * y2 - x2 * y1 - x1 * y3) == )
++cnt;
}
res = max(res, cnt);
}
res = max(res, same);
}
return res;
}
};

力扣算法题—149Max Points on a line的更多相关文章

  1. 力扣算法题—069x的平方根

    实现 int sqrt(int x) 函数. 计算并返回 x 的平方根,其中 x 是非负整数. 由于返回类型是整数,结果只保留整数的部分,小数部分将被舍去. 示例 1: 输入: 4 输出: 2 示例 ...

  2. 力扣算法题—060第K个排列

    给出集合 [1,2,3,…,n],其所有元素共有 n! 种排列. 按大小顺序列出所有排列情况,并一一标记,当 n = 3 时, 所有排列如下: "123" "132&qu ...

  3. 力扣算法题—050计算pow(x, n)

    #include "000库函数.h" //使用折半算法 牛逼算法 class Solution { public: double myPow(double x, int n) { ...

  4. 力扣算法题—147Insertion_Sort_List

    Sort a linked list using insertion sort. A graphical example of insertion sort. The partial sorted l ...

  5. 力扣算法题—093复原IP地址

    给定一个只包含数字的字符串,复原它并返回所有可能的 IP 地址格式. 示例: 输入: "25525511135" 输出: ["255.255.11.135", ...

  6. 力扣算法题—079单词搜索【DFS】

    给定一个二维网格和一个单词,找出该单词是否存在于网格中. 单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中“相邻”单元格是那些水平相邻或垂直相邻的单元格.同一个单元格内的字母不允许被重复使用. ...

  7. 力扣算法题—052N皇后问题2

    跟前面的N皇后问题没区别,还更简单 #include "000库函数.h" //使用回溯法 class Solution { public: int totalNQueens(in ...

  8. 力扣算法题—051N皇后问题

    #include "000库函数.h" //使用回溯法来计算 //经典解法为回溯递归,一层一层的向下扫描,需要用到一个pos数组, //其中pos[i]表示第i行皇后的位置,初始化 ...

  9. 力扣算法题—143ReorderList

    Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… You may not mod ...

随机推荐

  1. swoole 多进程共享数据

    进程作为程序执行过程中资源分配的基本单位,拥有独立的地址空间,同一进程的线程可以共享本进程的全局变量,静态变量等数据和地址空间,但进程之间资源相互独立.由于PHP语言不支持多线程,因此Swoole使用 ...

  2. 伪造请求头向url传递参数爬取百度默认翻译

    from urllib import request,parse import json # 翻译函数 def fanyi(msg): #参数封装 data = { "kw": c ...

  3. pyppeteer模块的基本使用

    pyppeteer模块的基本使用 引言 Selenium 在被使用的时候有个麻烦事,就是环境的相关配置,得安装好相关浏览器,比如 Chrome.Firefox 等等,然后还要到官方网站去下载对应的驱动 ...

  4. mysql中@ROW的使用

    一.从test02表中查询每个uid最早登录的前2天 表如下图所示: select * from (select uid,day, @ROW := END rn, @uuid:=uid from (s ...

  5. 解决div和img之间的空隙

    div盒子和img之间有空隙之前也遇到过几次这问题,今天又遇到了特地来总结下. 先上代码和效果图: <!doctype html><html lang="en"& ...

  6. Javascript基础四(数组,字符,对象,日期)

    第一节:数组 1.数组的概念及定义     可以存放一组数据:          当需要操作多个数据时: 2.数组的创建方式 var arr1 = [1,2,3]; //字面量方式 var arr2 ...

  7. 属性class与:class

    1.官方的案例 我们可以传给 v-bind:class 一个对象,以动态地切换 class <div v-bind:class="{ active: isActive }"& ...

  8. PID算法知识点博文收藏记录

    https://blog.csdn.net/Uncle_GUO/article/details/51367764 https://blog.csdn.net/HandsomeHong/article/ ...

  9. SPI子系统

    一.SPI子系统模型 三个组成部分: SPI核心:连通了SPI客户驱动.SPI主控制器驱动 SPI控制器驱动:驱动芯片中的SPI控制器 SPI的FLASH(客户驱动) 二.SPI控制器驱动分析 sta ...

  10. paper 143:人脸验证

    持续更新ing,敬请期待! 参考:http://blog.csdn.net/stdcoutzyx/article/details/42091205  1. DeepID人脸识别算法 香港中文大学的团队 ...