1. 首页
  2. 软件开发
  3. day-03 回旋镖的数量

day-03 回旋镖的数量

  • 发布于 2024-09-26
  • 1 次阅读


思路:
用map集合记录相同距离的组合数,HashMap,前一个值表示距离,后一个值表示组合数

解题方法:
用两次for循环,记录相同距离的组合数,统计出总的可行组合数,组合数*2即为答案

class Solution {

    public int numberOfBoomerangs(int[][] points) {

        int ans=0;
        for(int []p:points){

             HashMap<Integer,Integer> hashMap=new HashMap<>();
            for(int []q:points){

                if(p[0]==q[0]&&p[1]==q[1]) continue;
                int dis=(p[0]-q[0])*(p[0]-q[0])+(p[1]-q[1])*(p[1]-q[1]);
                int num=hashMap.getOrDefault(dis,0);
                ans=ans+num;
                hashMap.put(dis,num+1);
            }
        }
        return ans*2;
    }

}

注:本来暴力的,结果时间超时,看了答案,恍然大悟

原文链接: https://blog.csdn.net/qq_53568730/article/details/135466708