力扣算法题—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 ...
随机推荐
- 《单词的减法》state1~state17(200p)
单词的减法 2016.05.18 state 1 absent accessible accordingly accuracy/accurate acquaint/acquaintance adequ ...
- 面试题22:链表中倒数第k个节点
# -*- coding:utf-8 -*- # class ListNode: # def __init__(self, x): # self.val = x # self.next = None ...
- HTML页面隐藏值
<input id="menuId" type="hidden" hidden="hidden" />
- django4-模板,templates
如何使用templates呢? 在views.py文件中,函数或者方法通过return render(request,"userInfor.html",{"info_li ...
- java--二叉树解析及基本实现
一.二叉树的结构 在进行链表结构开发的过程之中,会发现所有的数据按照首尾相连的状态进行保存,那么 在进行数据查询时为了判断数据是否存在,这种情况下它所面对的时间复杂度就是"O(n)" ...
- 整理eclipse,升级jdk环境小记录
这2天在整理项目: 需要把eclipse 32位,jdk1.6 32位的更改为eclipse 64位,jdk1.8 64位版本的,于是我就在一台window7的电脑上直接操作,遇到了一下几点问题,记录 ...
- Java管理Cookie增删改查操作。
Cookie属性 了解这几个属性之后,就知道如何进行Cookie的操作了. name Cookie的名称. value Cookie的值. maxAge Cookie的失效时间,有以下几种值,默认为- ...
- mysql 性能调优 参数随写
set global innodb_buffer_pool_size = 12*1024*1024*1024;set global bulk_insert_buffer_size = 12582912 ...
- js转换成字符串
有两种方法: 1.对于boolean, number, string类型,可调用toString()方法 2.用String(var)方法 其中,第二种方法使用范围更广,可将没有toString()方 ...
- SQL数据库—<6>存储过程
Transact-SQL中的存储过程,非常类似于Java语言中的方法,它可以重复调用.当存储过程执行一次后,可以将语句缓存中,这样下次执行的时候直接使用缓存中的语句.这样就可以提高存储过程的性能. Ø ...