网站建设与网页设计课程,网站建设要准备什么资料,美容养生行业WordPress主题,wordpress显示加载耗时文章目录 1. 优质数对的总数 II 1. 优质数对的总数 II
题目链接 #x1f34e;该题涉及的小技巧#xff1a;#x1f425; #x1f427;①一次可以统计这个数的 两个因子 但是要注意 25 5 * 5#xff0c;这种情况 5 只能统计一次噢#x1f192; 解题思路: #x1f427… 文章目录 1. 优质数对的总数 II 1. 优质数对的总数 II
题目链接 该题涉及的小技巧 ①一次可以统计这个数的 两个因子 但是要注意 25 5 * 5这种情况 5 只能统计一次噢 解题思路: ① 题目的意思可以转换成求 nums1 中有多少个数可以整除 nums2 * k的数 ② 我们先把 nums1 的因子都求出来因为要整除 nums2 * knums1肯定有因子是在 nums2中才能整除 ③ 把 nums1的所有因子用哈希表存起来即可 代码实现
class Solution {
public:long long numberOfPairs(vectorint nums1, vectorint nums2, int k) {unordered_mapint, int counts;// 1.先统计 nums1 的因子for (auto x : nums1){if (x % k ! 0)continue;for (int d 1; d * d x; d ){if (x % d ! 0)continue;counts[d];// 一次统计两个因子 例如 12 的因子是 2 和 6if (d * d x){counts[x / d] ;}}}long long ans 0;for (int x : nums2)ans counts[x * k];return ans; }
};