力扣第399场周赛
A
本题和C题一致,只需要枚举一下因子既可以解决问题。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| class Solution { public: long long numberOfPairs(vector<int>& nums1, vector<int>& nums2, int k) { unordered_map<int, int> cnt; for (int x : nums1) { if (x % k) { continue; } x /= k; for (int d = 1; d * d <= x; d++) { if (x % d) { continue; } cnt[d]++; if (d * d!=x) { cnt[x / d]++; } } } long long ans = 0; for (int x : nums2) { ans += cnt.contains(x) ? cnt[x] : 0; } return ans; } };
|
B
模拟即可
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| class Solution { public: string compressedString(string word) { int n = word.size(); string ans = ""; int p = 0, q = 0, cnt = 0; while (p < n) { if ((word[p] != word[q]) || cnt == 9) { ans += to_string(cnt); ans += word[q]; q = p; cnt = 0; continue; } p++; cnt++; } ans += to_string(cnt); ans += word[q]; return ans; } };
|
C
可以发现与第一道题一致,复杂度没有区别。