17 lines ~106 ms Java


  • 9
    J
    public class Solution {
        public int numberOfBoomerangs(int[][] points) {
            int result = 0;
            HashMap<Integer,Integer> distMap = new HashMap<Integer,Integer>();
            for(int[] i : points) {
                for(int[] j : points) {
                    if(i==j) continue;
                    int dist = (i[0]-j[0])*(i[0]-j[0]) + (i[1]-j[1])*(i[1]-j[1]);
                    int prevDist = distMap.containsKey(dist) ? distMap.get(dist) : 0;
                    result += 2*prevDist;
                    distMap.put(dist, prevDist+1);
                }
                distMap.clear();
            }
            return result;
        }
    }
    

  • 0
    J

    @jarzeb said in 17 lines ~106 ms Java:

    result += 2*prevDist;

    Same idea. But only your solution uses it and save a loop compared to others.


  • 0
    G

    int prevDist = distMap.containsKey(dist) ? distMap.get(dist) : 0;

    if you are using java8, you could try

    int value = map.getOrDefault(key, 0);


Log in to reply
 

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