leetcode 447. Number of Boomerangs【hash】

447. Number of Boomerangs【hash】

You are given n points in the plane that are all distinct, where points[i] = [xi, yi]. A boomerang is a tuple of points (i, j, k) such that the distance between i and j equals the distance between i and k (the order of the tuple matters).

Return the number of boomerangs.

Example 1:

Input: points = [[0,0],[1,0],[2,0]]
Output: 2
Explanation: The two boomerangs are [[1,0],[0,0],[2,0]] and [[1,0],[2,0],[0,0]].
Example 2:

Input: points = [[1,1],[2,2],[3,3]]
Output: 2
Example 3:

Input: points = [[1,1]]
Output: 0

Constraints:

n == points.length
1 <= n <= 500
points[i].length == 2
-104 <= xi, yi <= 104
All the points are unique.

Java hash O(n^2)

class Solution {
    private int getDistance(int[] a, int[] b){
        int x=a[0]-b[0];
        int y=a[1]-b[1];
        return x*x+y*y;
    }

    public int numberOfBoomerangs(int[][] points) {
        int n=points.length;
        HashMap<Integer,Integer>[] distance=new HashMap[n];
        for(int i=0;i<n;i++){
            distance[i]=new HashMap<>();
        }

        for(int i=1;i<n;i++){
            for(int j=0;j<i;j++){
                int dis=getDistance(points[i],points[j]);

                int num=0;
                if(distance[i].containsKey(dis)){
                    num=distance[i].get(dis);
                }
                distance[i].put(dis,num+1);

                num=0;
                if(distance[j].containsKey(dis)){
                    num=distance[j].get(dis);
                }
                distance[j].put(dis,num+1);
            }
        }

        int ans=0;
        for(int i=0;i<n;i++){
            for(Integer dis:distance[i].keySet()){
                int num=distance[i].get(dis);
                //System.out.println(i+" "+dis+" "+num);
                ans+=(num-1)*num;
            }
        }

        return ans;
    }
}
  • java写得不太熟练,编译一直不通过
  • getOrDefault(key,0)
  • HashMap<Integer,Integer>[] arr=new HashMap[n];
  • 之所以想到hash是因为观察数据规模得出
  • Map.Entry<Integer,Interger> entry: map.entrySet() entry.getValue

c++ hash O(n^2)

class Solution {
public:
    int numberOfBoomerangs(vector<vector<int>> &points) {
        int ans = 0;
        for (auto &p : points) {
            unordered_map<int, int> cnt;
            for (auto &q : points) {
                int dis = (p[0] - q[0]) * (p[0] - q[0]) + (p[1] - q[1]) * (p[1] - q[1]);
                ++cnt[dis];
            }
            for (auto &[_, m] : cnt) {
                ans += m * (m - 1);
            }
        }
        return ans;
    }
};

作者:LeetCode-Solution
链接:https://leetcode-cn.com/problems/number-of-boomerangs/solution/hui-xuan-biao-de-shu-liang-by-leetcode-s-lft5/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
  • 感觉c++挺妙的
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值