LeetCode:149_Max Points on a line | 寻找一条直线上最多点的数量 | Hard
题目: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.
这道题需要稍微转变一下思路,用斜率来实现,试想找在同一条直线上的点,怎么判断在一条直线上,唯一的方式也只有斜率可以完成,我开始没想到,后来看网友的思路才想到的,下面是简单的实现:
其中有一点小技巧,利用map<double, int>来存储具有相同斜率值的的点的数量,简洁高效。
/*
Definition for a point
*/
// struct Point {
// int x;
// int y;
// Point():x(0),y(0) {}
// Point (int a, int b):x(0), y(0) {}
// }; int maxPoints(vector<Point> &points) {
if (points.empty())
return ;
if (points.size() <= )
return points.size(); int numPoints = points.size();
map<double, int> pmap; //存储斜率-点数对应值
int numMaxPoints = ; for (int i = ; i != numPoints - ; ++i) {
int numSamePoints = , numVerPoints = ; //针对每个点分别做处理 pmap.clear(); for (int j = i + ; j != numPoints; ++j) {
if(points[i].x != points[j].x) {
double slope = (double)(points[j].y - points[i].y) / (points[j].x - points[i].x);
if (pmap.find(slope) != pmap.end())
++ pmap[slope]; //具有相同斜率值的点数累加
else
pmap[slope] = ;
}
else if (points[i].y == points[j].y)
numSamePoints ++; //重合的点
else
numVerPoints ++; //垂直的点 }
map<double, int>::iterator it = pmap.begin();
for (; it != pmap.end(); ++ it) {
if (it->second > numVerPoints)
numVerPoints = it->second;
}
if (numVerPoints + numSamePoints > numMaxPoints)
numMaxPoints = numVerPoints + numSamePoints;
}
return numMaxPoints + ;
}
LeetCode:149_Max Points on a line | 寻找一条直线上最多点的数量 | Hard的更多相关文章
- [leetcode]Max Points on a Line @ Python
原题地址:https://oj.leetcode.com/problems/max-points-on-a-line/ 题意:Given n points on a 2D plane, find th ...
- LeetCode:Max Points on a Line
题目链接 Given n points on a 2D plane, find the maximum number of points that lie on the same straight l ...
- [LeetCode] Max Points on a Line 题解
题意 Given n points on a 2D plane, find the maximum number of points that lie on the same straight lin ...
- lintcode 中等题:Max Points on a Line 最多有多少个点在一条直线上
题目 最多有多少个点在一条直线上 给出二维平面上的n个点,求最多有多少点在同一条直线上. 样例 给出4个点:(1, 2), (3, 6), (0, 0), (1, 3). 一条直线上的点最多有3个. ...
- [LeetCode] 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. ...
- 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 on the ...
- 149. Max Points on a Line *HARD* 求点集中在一条直线上的最多点数
Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. ...
- [LintCode] Coins in a Line II 一条线上的硬币之二
There are n coins with different value in a line. Two players take turns to take one or two coins fr ...
- [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. ...
随机推荐
- 2、订单填写页面 /items/write?skuId=10&orderNo=201903211033410001
<template> <div class="write"> <div class="adr" v-if="addres ...
- php 获取数组深度的key
1.数组 深度遍历 function fun($a,&$b) { foreach ($a as $k=>$val) { if (is_array($val)) { $b[]=$k; fu ...
- 《Network Security A Decision and Game Theoretic Approach》阅读笔记
网络安全问题的背景 网络安全研究的内容包括很多方面,作者形象比喻为盲人摸象,不同领域的网络安全专家对网络安全的认识是不同的. For researchers in the field of crypt ...
- 复用微信小程序源码包后仍然有原小程序的版本管理怎么处理
前言: 复用微信小程序源码包后,重新创建项目导入源码包,会发现开发者工具版本管理中仍然有原来小程序的版本,这样就不太好了.毕竟是一个新的小程序,需要有新的版本控制的.那么这个问题怎么处理呢? 解决方案 ...
- MAC book 无法删除普通用户的解决办法
1来自苹果官网 macOS Sierra: 删除用户或群组 如果您是管理员,当您不想再让某些用户访问 Mac 时,可以删除他们.您也可以删除不想要的群组. 删除用户时,您可以存储该用户的个人文件夹(包 ...
- jq获取图片并转换为base64
html代码: <input type="file" id="file1"/> jq代码: $('#file1').change(function( ...
- Python集合(set)类型的操作 (转)
python的set和其他语言类似, 是一个无序不重复元素集, 基本功能包括关系测试和消除重复元素. 集合对象还支持union(联合), intersection(交), difference(差)和 ...
- 利用redis制作消息队列
redis在游戏服务器中的使用初探(一) 环境搭建redis在游戏服务器中的使用初探(二) 客户端开源库选择redis在游戏服务器中的使用初探(三) 信息存储redis在游戏服务器中的使用初探(四) ...
- 使用PreparedStatement向数据表中插入、修改、删除、获取Blob类型的数据
使用PreparedStatement向数据表中插入.修改.删除.获取Blob类型的数据 2014-09-07 20:17 Blob介绍 BLOB类型的字段用于存储二进制数据 MySQL中,BLOB是 ...
- 1-JRE与JDK等知识