LeetCode(149) Max Points on a Line
题目
Given n points on a 2D plane, find the maximum number of points that lie on the same straight line.
分析
求解一个二维平面上所有点中,位于同一直线上的最多点数。
首先想到的算法就是首先固定两个点求其斜率,然后在从剩余节点中计算该直线中的点,累计,比较、、、该方法时间复杂度要O(n^3),肯定不是最优解。
其实,可以略去一层循环,固定一个点,遍历剩余点,求每个斜率,借助一个map存储每个斜率上的点数,比较得到最大值。
斜率:
任意一条直线都可以表述为
y = ax + b
假设,有两个点(x1,y1), (x2,y2),如果他们都在这条直线上则有
y1 = kx1 +b
y2 = kx2 +b
由此可以得到关系,k = (y2-y1)/(x2-x1)。即如果点c和点a的斜率为k, 而点b和点a的斜率也为k,可以知道点c和点b也在一条线上。
注意:
1. points中重复出现的点。
2. int maxNum = 0;
初始化,以防points.size() ==0的情况。
3. mp[INT_MIN] = 0;
保证poins中只有一个结点,还有points中只有重复元素时,mp中没有元素。这两种极端情况。
4. int duplicate = 1;
duplicate记录重复点的数量,初始化为1,是因为要把当前的点points[i]加进去。
5. float k = points[i].x == points[j].x ? INT_MAX : (float)(points[j].y - points[i].y)/(points[j].x - points[i].x);
计算斜率,如果直线和y轴平行,就取INT_MAX,否则就取(float)(points[j].y - points[i].y)/(points[j].x - points[i].x)
AC代码
class Solution {
public:
int maxPoints(vector<Point>& points) {
if (points.empty())
return 0;
int size = points.size();
if (size < 3)
return size;
//记录最后在同一直线上的最多点数
int maxNum = 0;
//记录每条直线上的点数
map<float, int> line;
//固定一点,求其余另外所有点数构成的直线斜率
for (int i = 0; i < size; ++i)
{
line.clear();
line[INT_MIN] = 0;
//记录与当前节点的相同节点数
int common = 1;
for (int j = 0; j < size; ++j)
{
if (j == i)
continue;
//相同的两个点
else if (points[i].x == points[j].x && points[i].y == points[j].y)
{
++common;
continue;
}
else{
float k = (points[i].x == points[j].x) ? INT_MAX : (float)(points[i].y - points[j].y) / (points[i].x - points[j].x);
++line[k];
}//else
}//for
map<float, int>::iterator iter = line.begin();
for (; iter != line.end(); iter++)
{
if ((iter->second + common) > maxNum)
maxNum = iter->second + common;
}//for
}//for
return maxNum;
}
};
LeetCode(149) Max Points on a Line的更多相关文章
- 【LeetCode】149. Max Points on a Line
Max Points on a Line Given n points on a 2D plane, find the maximum number of points that lie on the ...
- 【leetcode】Max Points on a Line
Max Points on a Line 题目描述: Given n points on a 2D plane, find the maximum number of points that lie ...
- Leetcode(3)无重复字符的最长子串
Leetcode(3)无重复字符的最长子串 [题目表述]: 给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度. 第一种方法:暴力 执行用时:996 ms: 内存消耗:12.9MB 效果: ...
- Leetcode(5)最长回文子串
Leetcode(4)寻找两个有序数组的中位数 [题目表述]: 给定一个字符串 s,找到 s 中 最长 的回文子串.你可以假设 s 的最大长度为 1000.' 第一种方法:未完成:利用回文子串的特点 ...
- 新概念英语(1-49)At the butcher's
新概念英语(1-49)At the butcher's What does Mr. Bird like? A:Do you want any meat today, Mrs. Bird? B:Yes, ...
- LeetCode(275)H-Index II
题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimi ...
- LeetCode(220) Contains Duplicate III
题目 Given an array of integers, find out whether there are two distinct indices i and j in the array ...
- LeetCode(154) Find Minimum in Rotated Sorted Array II
题目 Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? W ...
- LeetCode(122) Best Time to Buy and Sell Stock II
题目 Say you have an array for which the ith element is the price of a given stock on day i. Design an ...
随机推荐
- github 新建一个分支
我能说今天在github上新建分支的时候懵逼了半天吗..为了下次不再懵逼,还是在这里记录一下吧.. 进入你的项目---code---Branch----点击那个倒三角-----你会发现一个输入框(这是 ...
- 写TXT文件
#region 写日志 private static void writelog(string strwrite) { string strPath = "d:/log.txt"; ...
- Java中的break循环——通过示例学习Java编程(13)
作者:CHAITANYA SINGH 来源:https://www.koofun.com//pro/kfpostsdetail?kfpostsid=24 break语句通常用于以下两种情况: (A)使 ...
- 在使用seek()函数时,有时候会报错为 “io.UnsupportedOperation: can't do nonzero cur-relative seeks”,代码如下:
__author__ = 'ZHHT' #!/usr/bin/env python # -*- coding:utf-8 -*- import os f = open("test1" ...
- C++析构函数造成Debug Assertion Failed的问题
昨天写了两个程序,均出现了析构函数造成Debug Assertion Failed的问题,由于是初学c++怎么想也想不通问题出在哪里.今天早上经人指点终于明白问题所在了.下面贴出代码和问题解析:(以下 ...
- MFC CDialog/CDialogEx DoModal ALT
Questions: I'm using MFC CDialog/CDialogEx to show a modal dialog with DoModal.usually it works with ...
- css隐藏元素的几种方法与区别
css隐藏元素的几种方法与区别 一:display:none;隐藏不占位 display 除了不能加入 CSS3 动画豪华大餐之外,基本效果卓越,没什么让人诟病的地方. 二:position:abso ...
- LoadRunner使用(1)
一.LoadRunner脚本录制 LoadRunner测试分为两个步骤: 第一步:录制脚本,其实就是监控并记录这段时间发送的HTTP请求 第二步:启动多个线程,用录制的脚本,模拟多线程发送请求. (1 ...
- JNI接口的使用(简单版)
详见 http://b6ec263c.wiz03.com/share/s/2SX2oY0nX4f32CY5ax1bapaL2Qtc5q0tIQjG2yfwaU1MX4Ye
- UVA11478 Halum (差分约束)
每次操作是独立的,而且顺序并不影响,作用在同一个结点上的d可以叠加,所以令x(u) = sigma(dui). 最后就是要确定所有的x(u). 因为m越大,满足条件的边就越少,二分答案m. 对于一条边 ...