这“给定2D平面上的n个点,找到位于同一直线上的最大点数。” 来自leetcode.com的问题我试图解决它,但我无法通过所有测试 例。
我想要做的是: - 我正在使用一个hashmap,其键是角度b / w,我通过斜率的tan反转得到的点,我存储的每个斜率的值最初的值是没有出现那个点,然后递增它。
我正在使用另一个hashmap来计算点的出现次数。
我没有得到像(0,0),(1,0)这样的点的正确答案,它应该返回2但是它返回1.
我错过了什么?
我的代码是:
public class MaxPointsINLine {
int max = 0;
int same;
public int maxPoints(Point[] points) {
int max = 0;
Map<Double, Integer> map = new HashMap<Double, Integer>();
Map<Point, Integer> pointmap = new HashMap<Point, Integer>();
for(Point point: points)
{
if(!pointmap.containsKey(point))
{
pointmap.put(point, 1);
}
else
{
pointmap.put(point, pointmap.get(point)+1);
}
}
if (points.length >= 2) {
for (int i = 0; i < points.length; i++) {
for (int j = i ; j < points.length; j++) {
double dx = points[j].x - points[i].x;
double dy = points[j].y - points[i].y;
double slope = Math.atan(dy / dx);
if (!map.containsKey(slope)) {
map.put(slope, pointmap.get(points[j]));
} else
map.put(slope, map.get(slope) + 1);
}
}
for (Double key : map.keySet()) {
if (map.get(key) > max) {
max = map.get(key);
}
}
return max;
} else if (points.length != 0) {
return 1;
} else {
return 0;
}
}
public static void main(String[] args) {
Point point1 = new Point(0,0);
Point point2 = new Point(0,0);
//Point point3 = new Point(2,2);
MaxPointsINLine maxpoint = new MaxPointsINLine();
Point[] points = { point1, point2};
System.out.println(maxpoint.maxPoints(points));
}
}
class Point {
int x;
int y;
Point() {
x = 0;
y = 0;
}
Point(int a, int b) {
x = a;
y = b;
}
@Override
public boolean equals(Object obj) {
Point that = (Point)obj;
if (that.x == this.x && that.y == this.y)
return true;
else
return false;
}
@Override
public int hashCode() {
// TODO Auto-generated method stub
return 1;
}
}
答案 0 :(得分:2)
总体战略似乎不起作用。我们假设我们已成功填充了map
的键值对(a, N)
,其中a
是一个角度,N
是对的数量由角度a
连接的点。考虑以下6点的安排:
**
**
**
明确地,在(0,0),(1,0),(2,1),(3,1),(0,2)和(1,2)处有点。你可以检查任何一行上的最大点数是2.但是,有3对以相同角度连接的点:((0,0),(1,0)),((2 ,1),(3,1))和((0,2),(1,2))。因此map
将包含与N = 3
的键值对,但这不是原始问题的答案。
由于上述安排,它不足以计算斜坡。一个成功的算法必须以这样一种方式表示线条,你可以区分平行线。
答案 1 :(得分:2)
这里最直接的解决方案似乎如下:人们可以考虑每对点。对于每对,计算每个其他点到连接两个点的线的距离。如果该距离几乎为零,则该点位于该线上。
当对所有对完成此操作时,可以选择连接线上具有最高点数的对。
当然,这不是很优雅,因为它在O(n ^ 2)中并执行两次计算。但它非常简单而且非常可靠。我刚刚在这里实现了MCVE:
可以通过左键单击鼠标来设置点。右键单击清除屏幕。显示一行中的最大点数,并突出显示相应的点。
(编辑:更新以根据评论处理距离为0的点)
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.geom.Line2D;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class PointsOnStraightLineTest
{
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
@Override
public void run()
{
createAndShowGUI();
}
});
}
private static void createAndShowGUI()
{
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().setLayout(new BorderLayout());
f.getContentPane().add(new PointsPanel());
f.setSize(600, 600);
f.setLocationRelativeTo(null);
f.setVisible(true);
}
}
class PointsOnStraightLine
{
// Large epsilon to make it easier to place
// some points on one line with the mouse...
private static final double EPSILON = 5.0;
List<Point> maxPointsOnLine = new ArrayList<Point>();
void compute(List<Point> points)
{
maxPointsOnLine = new ArrayList<Point>();
for (int i=0; i<points.size(); i++)
{
for (int j=i+1; j<points.size(); j++)
{
Point p0 = points.get(i);
Point p1 = points.get(j);
List<Point> resultPoints = null;
if (p0.distanceSq(p1) < EPSILON)
{
resultPoints = computePointsNearby(p0, points);
}
else
{
resultPoints = computePointsOnLine(p0, p1, points);
}
if (resultPoints.size() > maxPointsOnLine.size())
{
maxPointsOnLine = resultPoints;
}
}
}
}
private List<Point> computePointsOnLine(
Point p0, Point p1, List<Point> points)
{
List<Point> result = new ArrayList<Point>();
for (int k=0; k<points.size(); k++)
{
Point p = points.get(k);
double d = Line2D.ptLineDistSq(p0.x, p0.y, p1.x, p1.y, p.x, p.y);
if (d < EPSILON)
{
result.add(p);
}
}
return result;
}
private List<Point> computePointsNearby(
Point p0, List<Point> points)
{
List<Point> result = new ArrayList<Point>();
for (int k=0; k<points.size(); k++)
{
Point p = points.get(k);
double d = p.distanceSq(p0);
if (d < EPSILON)
{
result.add(p);
}
}
return result;
}
}
class PointsPanel extends JPanel implements MouseListener
{
private final List<Point> points;
private final PointsOnStraightLine pointsOnStraightLine;
PointsPanel()
{
addMouseListener(this);
points = new ArrayList<Point>();
pointsOnStraightLine = new PointsOnStraightLine();
}
@Override
protected void paintComponent(Graphics gr)
{
super.paintComponent(gr);
Graphics2D g = (Graphics2D)gr;
g.setColor(Color.BLACK);
for (Point p : points)
{
g.fillOval(p.x-2, p.y-2, 4, 4);
}
pointsOnStraightLine.compute(points);
int n = pointsOnStraightLine.maxPointsOnLine.size();
g.drawString("maxPoints: "+n, 10, 20);
g.setColor(Color.RED);
for (Point p : pointsOnStraightLine.maxPointsOnLine)
{
g.drawOval(p.x-3, p.y-3, 6, 6);
}
}
@Override
public void mouseClicked(MouseEvent e)
{
if (SwingUtilities.isRightMouseButton(e))
{
points.clear();
}
else
{
points.add(e.getPoint());
}
repaint();
}
@Override
public void mousePressed(MouseEvent e)
{
}
@Override
public void mouseReleased(MouseEvent e)
{
}
@Override
public void mouseEntered(MouseEvent e)
{
}
@Override
public void mouseExited(MouseEvent e)
{
}
}
答案 2 :(得分:2)
这个对我有用:
/**
* 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 result = 0;
for(int i = 0; i<points.length; i++){
Map<Double, Integer> line = new HashMap<Double, Integer>();
Point a = points[i];
int same = 1;
//System.out.println(a);
for(int j = i+1; j<points.length; j++){
Point b = points[j];
//System.out.println(" point " + b.toString());
if(a.x == b.x && a.y == b.y){
same++;
} else {
double dy = b.y - a.y;
double dx = b.x - a.x;
Double slope;
if(dy == 0){ // horizontal
slope = 0.0;
} else if(dx == 0){//eartical
slope = Math.PI/2;
} else {
slope = Math.atan(dy/dx);
}
Integer slopeVal = line.get(slope);
//System.out.println(" slope " + slope + " slope value " + slopeVal);
if(slopeVal == null){
slopeVal = 1;
} else {
slopeVal += 1;
}
line.put(slope, slopeVal);
}
}
for (Double key : line.keySet()) {
Integer val = line.get(key);
result = Math.max(result, val + same);
}
// for all points are same
result = Math.max(result, same);
}
return result;
}
}