|  | 
 
| 
题目描述:
x
马上注册,结交更多好友,享用更多功能^_^您需要 登录 才可以下载或查看,没有账号?立即注册  
 复制代码给定平面上 n 对 互不相同 的点 points ,其中 points[i] = [xi, yi] 。回旋镖 是由点 (i, j, k) 表示的元组 ,其中 i 和 j 之间的距离和 i 和 k 之间的距离相等(需要考虑元组的顺序)。
返回平面上所有回旋镖的数量。
 
示例 1:
输入:points = [[0,0],[1,0],[2,0]]
输出:2
解释:两个回旋镖为 [[1,0],[0,0],[2,0]] 和 [[1,0],[2,0],[0,0]]
示例 2:
输入:points = [[1,1],[2,2],[3,3]]
输出:2
示例 3:
输入:points = [[1,1]]
输出:0
 
提示:
n == points.length
1 <= n <= 500
points[i].length == 2
-104 <= xi, yi <= 104
所有点都 互不相同
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/number-of-boomerangs
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
 
 复制代码class Solution {
public:
    int numberOfBoomerangs(vector<vector<int>>& points) {
        int res = 0;
        int len = points.size();
        for(int i = 0; i < len; i++){
            map<int, int>store;//关键,假设每次固定一个i元素,看其他两个元素的选择
            for(int j = 0; j < len; j++){//j和i相等也不影响,因为store【0】=1,使得(1*0)=0
                int temp1 = pow((points[i][0] - points[j][0]), 2);
                int temp2 = pow((points[i][1] - points[j][1]), 2);
                store[temp1 + temp2]++;
            }
            for(auto cha : store){
                res += cha.second * (cha.second - 1);
            }
        }
        return res;
    }
};
 | 
 |