Leetcode--->Given n points on a 2D plane, find the maximum number of points that lie on the same str
来源:互联网 发布:spss软件下载 官方 编辑:程序博客网 时间:2024/05/21 09:00
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) { //关键在于判断三点共线,两平行直线有且只有一个交点,所以有一个中间点,这个中间点与另外两个端点的连线的斜率相等 //由比率的性质 int ABx; int ABy; int BCx; int BCy; if(points.length<=2) return points.length; int max=2;//用来记录最大个数 for(int i=0;i<points.length;i++){ int num=0; int temp=1; for(int j=i+1;j<points.length;j++){ ABx=points[i].x-points[j].x; ABy=points[i].y-points[j].y; if(ABx==0 && ABy==0)//表示出现重复点 { num++; }else{ temp++; for(int k=j+1;k<points.length;k++){ BCx=points[j].x-points[k].x; BCy=points[j].y-points[k].y; if(ABx*BCy==BCx*ABy){//表示两个斜率相等,转化为乘积的形式可以避免分母为0的情况 temp++; } } } if(max<(num+temp)){ max=num+temp; } temp=1; } } return max; }}
0 0
- Leetcode--->Given n points on a 2D plane, find the maximum number of points that lie on the same str
- Given n points on a 2D plane, find the maximum number of points that lie on the same straight line.
- 题目描述 Given n points on a 2D plane, find the maximum number of points that lie on the same straight
- [LeetCode]Maximum number of points on a straight line in 2d plane
- Given a sequence of numbers (or array).Find the maximum distance between all the same numbers.
- (leetcode 2) Max Points on a line
- Codeforces Round #319 (Div. 2)E Points on Plane
- codeforces 576C Points on Plane
- Max Points on a Line | leetcode
- Max Points on a Line LEETCODE
- Leetcode: Max Points on a Line
- LeetCode - Max Points on a Line
- LeetCode 之 Max Points on a Line
- LeetCode | Max Points on a Line
- [LeetCode] Max Points on a Line
- Leetcode: Max Points on a Line
- leetcode _Max Points on a Line
- LeetCode OJ:Max Points on a Line
- 读nodejs项目源码所遇问题小结(一)
- zoj3471 Most Powerful 状压dp
- 【Java基础】RTTI与反射之Java
- 最大熵模型介绍及实现
- Web Service系列之实例之JAX-WS
- Leetcode--->Given n points on a 2D plane, find the maximum number of points that lie on the same str
- JS的几种写法
- Koa学习笔记
- 当类对象作为成员看初始化列表的意义
- 【小型系统】抽奖系统-使用Java Swing完成
- 二叉树总结
- 教你如何迅速秒杀掉:99%的海量数据处理面试题
- CFgym:Database(字符串处理)
- lightoj 1128 倍增法+dp