Why my code did not pass the OJ? Thanks.


  • 0
    A
    public class Solution {
        public boolean isReflected(int[][] points) {
    
            Map<Integer, List<Integer>> map = new HashMap<Integer, List<Integer>>();
            for (int[] point: points) {
                if (!map.containsKey(point[1])) {
                    map.put(point[1], new ArrayList<Integer>());
                }
                map.get(point[1]).add(point[0]);
    
            }
            Double possible = null;
            for(Integer key: map.keySet()) {
                List<Integer> list = map.get(key);
                Collections.sort(list);
                int l = 0;
                int r = list.size() - 1;
                while(l <= r) {
                    double tmp = (list.get(l) + list.get(r)) * 1.0 /2.0;
                    if (possible != null && (possible - tmp) > 0.00000001)
                        return false;
                    possible = tmp;
                    ++l;
                    --r;
                }
    
            }
            return true;
        }
    
    
    }
    

    Basically, OJ says that this code didn't pass this test case [[1,1],[-1,-1]], but on my local machine, it return false. Could anyone help, thanks.


  • 0
    L

    You need to add Absolute Value.
    Also, in this problem, I think you can just use equals, since numbers can always be divided by 2.


Log in to reply
 

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