Java beat97%


  • 0
    W

    '''
    class Line{
    float a;
    float b;
    float c;
    Line(float i, float j, float k) {a = i; b = j; c = k;}

    @Override
    public boolean equals(Object obj) {
        Line l = (Line)obj;
        return ((a-l.a)<0.000001 && (b-l.b)<0.0000001 && (c-l.c)<0.0000001);
    }
    
    public int hashCode() {
        return (int)(31*((a*10 * 31) + b*10) + c);
    }
    
    public String toString(){
        return a + " " + b + " " + c;
    }
    

    }

    public class Solution {

    private Line getLine(Point p1, Point p2) {
        if(p1.x == p2.x)
            return new Line(1, 0, -1*p1.x);
            
        if(p1.y == p2.y)
            return new Line(0, 1, -1*p1.y);
            
        if((p1.x*p2.y - p2.x*p1.y) == 0) {
            if(p1.x == 0) {
                return new Line(-1*p2.y/p2.x, 1, 0);
            } else {
                return new Line(-1*p1.y/p1.x, 1, 0);
            }
        }
    
        float a = (float)(p1.y - p2.y) / (float)(p1.x*p2.y - p2.x*p1.y);
        float b = (float)(p2.x - p1.x) / (float)(p1.x*p2.y - p2.x*p1.y);
        float c = 1;
        return new Line(a, b, c);
    }
    
    public int maxPoints(Point[] points) {
        int n = points.length;
        if(n == 0) return 0;
        if(n == 1) return 1;
    
        int maxNum = 2;
    
        for(int i = 0; i < n; i++) {
            Map<Line, Integer> res = new HashMap();
            int count = 0;
            int tempmax = 2;
            boolean flg = false;
            
            for(int j = i+1; j < n; j++) {
                if (points[i].x == points[j].x && points[i].y == points[j].y) {
                    count++;       // same point(point[i])
                } else {
                    Line tempLine = getLine(points[i], points[j]);
                    if (res.get(tempLine) == null) {
                        res.put(tempLine, 2);
                        flg = true;
                    } else {
                        res.put(tempLine, res.get(tempLine) + 1);
                        tempmax = Math.max(res.get(tempLine), tempmax);
                    }
                }
            }
            maxNum = Math.max(maxNum, flg?(tempmax + count): (count+1));
        }
        return maxNum;
    }'''
    

    }'''


  • 0
    W

    return (Math.abs(a-l.a)<0.000001 && Math.abs(b-l.b)<0.0000001 && Math.abs(c-l.c)<0.0000001);
    sorry……


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.