leetcode 149. Max Points on a Line --------- java
Given n points on a 2D plane, find the maximum number of points that lie on the same straight line.
/**
* Definition for a point.
* class Point {
* int x;
* int y;
* Point() { x = 0; y = 0; }
* Point(int a, int b) { x = a; y = b; }
* }
*/
public class Solution {
public int maxPoints(Point[] points) {
if (points.length < 3) return points.length; int max = 0;//用于返回的结果,即共线点的最大个数
Map<Double, Integer> map = new HashMap<Double, Integer>();//保存同一个斜率的点的个数 for (int i = 0; i < points.length; i++) {//以每一个点为固定点
map.clear();
int duplicate = 1;//记录跟固定点重合的个数 for(int j = i+1 ; j < points.length; j++){//遍历其他点,求其与固定点之间的斜率
double slope = 0.0;//斜率 if (points[i].x == points[j].x && points[i].y == points[j].y) {//如果跟固定点重合
duplicate++;
continue;
} else if (points[i].x == points[j].x) {//如果跟固定点在同一条竖线上,斜率设为最大值
slope = Integer.MAX_VALUE;
} else if( points[i].y == points[j].y ){//计算该点与固定点的斜率
slope = 0;
} else{
slope = 1.0 * (points[i].y - points[j].y) / (points[i].x - points[j].x);
}
map.put(slope, map.containsKey(slope) ? map.get(slope) + 1 : 1);
} //更新最优解
if (map.keySet().size() == 0) {//如果map为空
max = duplicate > max ? duplicate : max;
} else {
for (double key : map.keySet()) {
max = Math.max((duplicate + map.get(key)), max);
}
}
}
return max;
}
}
leetcode 149. Max Points on a Line --------- java的更多相关文章
- [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. ...
- Java for 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. ...
- [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. ...
- 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. ...
- 【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 OJ] Max Points on a Line
Max Points on a Line Submission Details 27 / 27 test cases passed. Status: Accepted Runtime: 472 ms ...
- 【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】149. Max Points on a Line 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典+最大公约数 日期 题目地址:https://l ...
- 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 li ...
随机推荐
- java抽象类与接口 详解
在面向对象的概念中,我们知道所有的对象都是通过类来描绘的,但是并不是所有的类都是用来描绘对象的,如果一个类中没有包含足够的信息来描绘一个具体的对象,这样的类就是抽象类. 抽象类往往用来表征我们在对问题 ...
- 使用siege进行Web压力测试
因为最近公司线上node项目出一些不稳定的情况,考虑在这方面能不能做进一步的优化,既然要做优化首先要知道问题出在哪里? 因为暂无法定位是node层问题还是api层出现问题,由于在开发环境小并发量程序运 ...
- Installing Cygwin and Starting the SSH Daemon
This chapter explains how to install Cygwin and start the SSH daemon on Microsoft Windows hosts. Thi ...
- config、make、make install
.config/ .configure (查看该目录下是否有这个文件,如果有makefile,可直接make) 配置 config是一个shell脚本,根据平台的特性生成Makefile文件 ...
- C# 跨线程操作控件(简洁)
C# 跨线程操作控件 .net 原则上禁止跨线程访问控件,因为这样可能造成错误的发生.解决此问题的方法有两个: 第一 ...
- 转载--Ubuntu设置环境变量
Ubuntu设置环境变量并立即生效(以Ubuntu12.04为例) 标签: UbuntuLinux环境变量 2013-09-12 19:04 9961人阅读 评论(1) 收藏 举报 分类: Ubun ...
- 2016 - 1 - 22 HTTP(二)
一: 发送HTTP请求的方法 1.在HTTP/1.1中规定了8种发送请求的方法: 2.发送请求时需要参数,比如POST中的账号密码 二:POST与GET的对比 1.GET与POST的主要区别表现在数据 ...
- GeekPwn2015胸卡ESP8266 12E串口调试
相信今年参加GeekPwn活动的很多同学都获得了一枚GeekPwn的胸牌,为方便大家对胸牌进行一些调试和破解,这里分享一些关于这枚胸牌的一些信息 :)如发现文章之中有错误之处,欢迎大家斧正! 0×00 ...
- 简单学习:repo入门
一:关于repo repo是Google开发的用于管理Android版本库的一个工具,repo并不是用于取代git,而是用Python对git进行了一定的封装,简化了对多个Git版本库的管理.对于re ...
- 在android的spinner中,实现取VALUE值和TEXT值。 ZT
在android的spinner中,实现取VALUE值和TEXT值. 为了实现在android的 spinner实现取VALUE值和TEXT值,我尝试过好些办法,在网上查的资料,都是说修改适配器, ...