题目

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;
}
};

GitHub测试程序源码

LeetCode(149) Max Points on a Line的更多相关文章

  1. 【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 ...

  2. 【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 ...

  3. Leetcode(3)无重复字符的最长子串

    Leetcode(3)无重复字符的最长子串 [题目表述]: 给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度. 第一种方法:暴力 执行用时:996 ms: 内存消耗:12.9MB 效果: ...

  4. Leetcode(5)最长回文子串

    Leetcode(4)寻找两个有序数组的中位数 [题目表述]: 给定一个字符串 s,找到 s 中 最长 的回文子串.你可以假设 s 的最大长度为 1000.' 第一种方法:未完成:利用回文子串的特点 ...

  5. 新概念英语(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, ...

  6. LeetCode(275)H-Index II

    题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimi ...

  7. LeetCode(220) Contains Duplicate III

    题目 Given an array of integers, find out whether there are two distinct indices i and j in the array ...

  8. LeetCode(154) Find Minimum in Rotated Sorted Array II

    题目 Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? W ...

  9. 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 ...

随机推荐

  1. 健康检查NET Core之跨平台的实时性能监控

    ASP.NET Core之跨平台的实时性能监控(2.健康检查)   前言 上篇我们讲了如何使用App Metrics 做一个简单的APM监控,最后提到过健康检查这个东西. 这篇主要就是讲解健康检查的内 ...

  2. Net Core迁移到MSBuild

    Net Core迁移到MSBuild平台(二)   阅读目录 一.前言 二.XML定义 三.结语 回到目录 一.前言 在上一篇文章.Net Core迁移到MSBuild的多平台编译问题中,简单的讲了下 ...

  3. WPF 模拟Button按钮事件触发

    this.Submit.AddHandler(Button.ClickEvent, new RoutedEventHandler(this.Submit_Click)); //这种是无效的方法 thi ...

  4. js 回车提交表单

    一.整个页面用一个回车提交事件: <input type="button" value="回车提交" id="auto" onclic ...

  5. Linux mount实际使用

    查看所有文件系统(设备):fdisk -l 1.当要重新挂载一个文件系统时(设备):可以直接 #mount -o remount,rw /dev/sdb9/(文件系统) /mnt/sdb9/(目录) ...

  6. Mysql order by 排序 varchar 类型数据

    Mysql order by 排序 varchar 类型数据 varchar 类型字段排序,  会將数字当成字符串来处理.  排序规则一般是从左到右一位位来比较. +0之后 就转化成INT 类型排序 ...

  7. SpringBoot 封装返回类以及session 添加获取

    1.创建返回类Result public class Result<T>{ /*错误码*/ private Integer code; /*提示信息 */ private String m ...

  8. jsp-简单的猜数小游戏

    <%@ page language="java" import="java.util.*" pageEncoding="utf-8"% ...

  9. webAPP制作框架Ionic--构建APP侧边栏 底部选项卡 轮播图 加载动画

    超好用的移动框架--Ionic Ionic是一个轻量的手机UI库,具有速度快,界面现代化.美观等特点. 为了解决其他一些UI库在手机上运行缓慢的问题,它直接放弃了IOS6和Android4.1以下的版 ...

  10. jQuery toggle 使用

    jQuery 中 toggle 作用 切换元素的显示与隐藏状态 如果被选元素可见,则隐藏这些元素,如果被选元素隐藏,则显示这些元素. <body> <div class=" ...