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. JNDI+Tomcat配置数据源的两种方式

    非全局jndi配置步骤 :此种配置方式不需要在server.xml中配置数据源,而只在tomcat/conf/Catalina/localhost下的启动配置中配置即可.注意红色字体名称必须和相同. ...

  2. SAP Smartforms打印输出条形码 及相关问题

    最近凭证打印需要附加打印条形码,遂做了一个小例子,结果还出现了很多的小问题,按领导的话说,这就是经验! 首先:SE73 -> 系统条形码 -> 更改 -> 创建 -> 选择 N ...

  3. Vue-Cli3环境安装

    一,安装node环境 尽量使用高版本的node环境,低版本的node环境会出现各种安装问题 下载地址: http://nodejs.cn/download/ 打开cmd node -v :查看node ...

  4. 迷你redux实现,redux是如何进行实现的?

    export function createStore(reducer){ let currentState={} let currentListeners=[] function getState( ...

  5. setquota - 设置磁盘配额或时间限制

    SYNOPSIS(总览) setquota [ -u | -g ] filesystem-name block-soft block-hard inode-soft inode-hard name.. ...

  6. xshell xftp使用

    1.xftp传输的中文上去乱码,是因为传输时使用GB2312,而服务端不是GB2312 使用UTF-8编码上传即可

  7. pip 批量安装包

    1 python3环境已经安装好,且也配置到环境变量:这种方式是在线安装 注意不要将   pip list  也安装了了,不然可能会覆盖自己已安装的这个包 首先,在已配置好的一台机器上,将需要的包导出 ...

  8. Java中volatile如何保证long和double的原子性操作

    原创转载请注明出处:https://www.cnblogs.com/agilestyle/p/11426473.html 关键字volatile的主要作用是使变量在多个线程间可见,但无法保证原子性,对 ...

  9. python学习--第二天 爬取王者荣耀英雄皮肤

    今天目的是爬取所有英雄皮肤 在爬取所有之前,先完成一张皮肤的爬取 打开anacond调出编译器Jupyter Notebook 打开王者荣耀官网 下拉找到位于网页右边的英雄/皮肤 点击[+更多] 进入 ...

  10. vue 路由动态传参 (多个)

    动态传参 传值页面  客户列表clientList.vue 路由 router.js 配置路由 接收参数的页面  客户详情CustomerDetails.vue 通过this.$router.para ...