力扣算法题—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 ...
随机推荐
- 美化Windows
更改壁纸 https://www.omgubuntu.co.uk/2010/09/a-look-back-at-every-ubuntu-default-wallpaper google: ubunt ...
- Linux系统CPU占用率较高问题排查思路
作为 Linux 运维工程师,在日常工作中我们会遇到 Linux服务器上出现CPU负载达到100%居高不下的情况,如果CPU 持续跑高,则会影响业务系统的正常运行,带来企业损失. 很多运维的同学遇到这 ...
- QTP read or write XML file
'strNodePath = "/soapenv:Envelope/soapenv:Body/getProductsResponse/transaction/queryProducts/qu ...
- 一些关于SEO优化的笔记
高级搜索指令: 双引号:“xxx” 代表完全匹配的搜索 减号:-(减号前面必须是空格,后面必须没有空格)代表搜索不包含减号后面的词的页面 filetype:用于搜索特定文件格式(百度支持的文件类型:P ...
- 转载:LESS基本用法
转载出处:https://blog.csdn.net/qq_38209578/article/details/80566860 转载出处:https://blog.csdn.net/weixin_44 ...
- Springboot03-异常处理
springboot默认异常处理 Spring Boot提供了一个默认的映射:/error,当处理中抛出异常之后,会转到该请求中处理,并然后返回一个固定的错误页面 统一异常处理 创建全局异常处理类 @ ...
- leetcode.排序.75颜色分类-Java
1. 具体题目 给定一个包含红色.白色和蓝色,一共 n 个元素的数组,原地对它们进行排序,使得相同颜色的元素相邻,并按照红色.白色.蓝色顺序排列.此题中,我们使用整数 0. 1 和 2 分别表示红色. ...
- WebSocket 网页聊天室
先给大家开一个原始的websocket的连接使用范例 <?php /* * recv是从套接口接收数据,也就是拿过来,但是不知道是什么 * read是读取拿过来的数据,就是要知道recv过来的是 ...
- adb]ADB server didn't ACK
遇到上述问题 此时由于不正常退出 会在进程中遗留Android debug进程 ,需要强制删除
- JS字符串和数组之间的转换
1.字符串转换为数组 var string = '123,456,789'; var stringResult = string.split(','); console.log(stringResul ...