糖逗 发表于 2021-1-18 13:29:22

C++刷LeetCode(1726. 同积元组)【数学】

题目描述:

给你一个由 不同 正整数组成的数组 nums ,请你返回满足 a * b = c * d 的元组 (a, b, c, d) 的数量。其中 a、b、c 和 d 都是 nums 中的元素,且 a != b != c != d 。



示例 1:

输入:nums =
输出:8
解释:存在 8 个满足题意的元组:
(2,6,3,4) , (2,6,4,3) , (6,2,3,4) , (6,2,4,3)
(3,4,2,6) , (4,3,2,6) , (3,4,6,2) , (4,3,6,2)
示例 2:

输入:nums =
输出:16
解释:存在 16 个满足题意的元组:
(1,10,2,5) , (1,10,5,2) , (10,1,2,5) , (10,1,5,2)
(2,5,1,10) , (2,5,10,1) , (5,2,1,10) , (5,2,10,1)
(2,10,4,5) , (2,10,5,4) , (10,2,4,5) , (10,2,4,5)
(4,5,2,10) , (4,5,10,2) , (5,4,2,10) , (5,4,10,2)
示例 3:

输入:nums =
输出:40
示例 4:

输入:nums =
输出:0


提示:

1 <= nums.length <= 1000
1 <= nums <= 104
nums 中的所有元素 互不相同


class Solution {
public:
    int tupleSameProduct(vector<int>& nums) {
      map<int,int>store;
      int len = nums.size();
      for(int i = 0; i < len; i++){
            for(int j = i + 1; j < len; j++){
                int temp = nums * nums;
                store++;
            }
      }
      int res = 0;
      for(auto cha : store){
            res += cha.second * (cha.second - 1) / 2;
      }
      //8 = 2 * 2 * 2表示其中一个二元组内部排序 * 另一个二元组内部排序 * 两个二元组整体排序
      return res * 8;
    }
};
页: [1]
查看完整版本: C++刷LeetCode(1726. 同积元组)【数学】