糖逗 发表于 2020-12-13 13:36:40

C++刷LeetCode(447. 回旋镖的数量)【数学】【排列组合】

题目描述:
给定平面上 n 对 互不相同 的点 points ,其中 points = 。回旋镖 是由点 (i, j, k) 表示的元组 ,其中 i 和 j 之间的距离和 i 和 k 之间的距离相等(需要考虑元组的顺序)。

返回平面上所有回旋镖的数量。

 
示例 1:

输入:points = [,,]
输出:2
解释:两个回旋镖为 [,,] 和 [,,]
示例 2:

输入:points = [,,]
输出:2
示例 3:

输入:points = []
输出:0
 

提示:

n == points.length
1 <= n <= 500
points.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 - points), 2);
                int temp2 = pow((points - points), 2);
                store++;
            }
            for(auto cha : store){
                res += cha.second * (cha.second - 1);
            }
      }
      return res;
    }
};
页: [1]
查看完整版本: C++刷LeetCode(447. 回旋镖的数量)【数学】【排列组合】