My code has no problem(works well on IDE), but cannot work on LeetCode.


  • 0
    L

    Hi, My code cannot work on leetcode, it gave me a Complie Error "error: cannot find symbol: method sort(Comparator)", but it works well in my Intellij IDEA.

    Here is the code.

    public class Solution {
    
            private Double getSlope(Point point1, Point point2) {
                if (point1.x == point2.x) {
                    if (point1.y > point2.y) {
                        return Double.NEGATIVE_INFINITY;
                    } else if (point1.y < point2.y) {
                        return Double.NEGATIVE_INFINITY;
                    } else {
                        return Double.MIN_VALUE;
                    }
                }
                return (double) (point2.y - point1.y) / (double) (point2.x - point1.x);
            }
    
            private int getMaximum(ArrayList<Point> pointArrayList, Point point) {
                int max = 0;
                double flag = Double.MIN_VALUE;
                int counter = 0;
                for (int i = 0; i < pointArrayList.size(); i++) {
                    double slope = getSlope(pointArrayList.get(i), point);
                    if (flag != slope) {
                        if (counter > max) {
                            max = counter;
                        }
                        flag = slope;
                        counter = 1;
                    } else {
                        counter++;
                    }
                }
                if (counter > max) {
                    max = counter;
                }
                return max;
            }
    
            public int maxPoints(Point[] points) {
                int maxNumber = 0;
    
                ArrayList<Point> pointArrayList = new ArrayList<Point>();
                for (int i = 0; i < points.length; i++) {
                    pointArrayList.add(points[i]);
                }
                for (int i = 0; i < points.length; i++) {
                        pointArrayList.sort(new Comparator<Point>() { //**Compile Error here.**
    
                        private Point point;
    
                        public Comparator setPoint(Point point) {
                            this.point = point;
                            return this;
                        }
    
                        @Override
                        public int compare(Point o1, Point o2) {
                            return getSlope(point, o1).compareTo(getSlope(point, o2));
                        }
                    }.setPoint(points[i]));
    
                    int number = getMaximum(pointArrayList, points[i]);
                    if (number > maxNumber) {
                        maxNumber = number;
                    }
                }
                return maxNumber;
            }
        }
    

    Thank you.


  • 0
    S

    Not familiar with java, but change to below won't CE any more.

    Collections.sort(pointArrayList, new Comparator<Point>() { //**Compile Error here.**
    
                        private Point point;
    
                        public Comparator setPoint(Point point) {
                            this.point = point;
                            return this;
                        }
    
                        @Override
                        public int compare(Point o1, Point o2) {
                            return getSlope(point, o1).compareTo(getSlope(point, o2));
                        }
                    }.setPoint(points[i]))

  • 0
    L

    Thank you so much, I will try that.


Log in to reply
 

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