力扣算法题—149Max Points on a line
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的更多相关文章
- 力扣算法题—069x的平方根
实现 int sqrt(int x) 函数. 计算并返回 x 的平方根,其中 x 是非负整数. 由于返回类型是整数,结果只保留整数的部分,小数部分将被舍去. 示例 1: 输入: 4 输出: 2 示例 ...
- 力扣算法题—060第K个排列
给出集合 [1,2,3,…,n],其所有元素共有 n! 种排列. 按大小顺序列出所有排列情况,并一一标记,当 n = 3 时, 所有排列如下: "123" "132&qu ...
- 力扣算法题—050计算pow(x, n)
#include "000库函数.h" //使用折半算法 牛逼算法 class Solution { public: double myPow(double x, int n) { ...
- 力扣算法题—147Insertion_Sort_List
Sort a linked list using insertion sort. A graphical example of insertion sort. The partial sorted l ...
- 力扣算法题—093复原IP地址
给定一个只包含数字的字符串,复原它并返回所有可能的 IP 地址格式. 示例: 输入: "25525511135" 输出: ["255.255.11.135", ...
- 力扣算法题—079单词搜索【DFS】
给定一个二维网格和一个单词,找出该单词是否存在于网格中. 单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中“相邻”单元格是那些水平相邻或垂直相邻的单元格.同一个单元格内的字母不允许被重复使用. ...
- 力扣算法题—052N皇后问题2
跟前面的N皇后问题没区别,还更简单 #include "000库函数.h" //使用回溯法 class Solution { public: int totalNQueens(in ...
- 力扣算法题—051N皇后问题
#include "000库函数.h" //使用回溯法来计算 //经典解法为回溯递归,一层一层的向下扫描,需要用到一个pos数组, //其中pos[i]表示第i行皇后的位置,初始化 ...
- 力扣算法题—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 ...
随机推荐
- SSH服务搭建、账号密码登录远程Linux虚拟机、基于密钥的安全验证(Windows_Xshell,Linux)
问题1:如果是两台虚拟机ping不同且其中一个虚拟机是克隆的另一个,需要更改一下MAC地址,关机状态下 一> "编辑虚拟机设置" 一>" 网络适配器" ...
- 未找到源文件:C:\loadrunner-11\urunner MSI\bin\icudt36.dll.o1d解决方法
安装HP LoadRunner 11.00 未找到源文件:C:\loadrunner-11\urunner MSI\bin\icudt36.dll.o1d 下载loadrunner11 使用迅雷下载, ...
- python面试题之请谈谈.pyc文件和.py文件的不同之处
虽然这两种文件均保存字节代码,但.pyc文件是Python文件的编译版本,它有平台无关的字节代码,因此我们可以在任何支持.pyc格式文件的平台上执行它.Python会自动生成它以优化性能(加载时间,而 ...
- Codeforces 497B Tennis Game( 枚举+ 二分)
B. Tennis Game time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...
- Autofac基本使用
原文:Autofac基本使用 AutoFac是.net平台下的IOC容器产品,它可以管理类之间的复杂的依赖关系.在使用方面主要是register和resolve两类操作. 这篇文章用单元测试的形式列举 ...
- Visual Studio Code如何编写运行C、C++
Visual Studio Code如何编写运行C.C++ 作者:知乎用户链接:https://www.zhihu.com/question/30315894/answer/154979413来源:知 ...
- k8s的存储卷
存储卷查看:kubectl explain pods.spec.volumes 一.简单的存储方式 1)2个容器之间共享存储..(删除则数据消失) apiVersion: v1 kind: Pod m ...
- C常量与变量
/** * C中的常量与变量 * 常量的值在程序中是不可变化的,其在定义时必须给一个初始值 * 常量的定义方式: * 1.#define 定义宏常量 * 2.const 定义const常量 * 对于# ...
- mysql中关于--login-path使用
在控制台登陆数据库,快捷登录 在控制台连接数据库,需要每次输入账号密码,感觉很麻烦,偶然发现可以通过login-path保存信息,实现快捷登录,这里记录下. 保存账号信息 mysql_config_e ...
- linux CentOS7 nginx nginx-rtmp-module搭建直播
直播配置 1. 安装 Nginx 依赖软件 yum -y install gcc gcc-c++ autoconf automake make yum -y install zlib zlib-dev ...