本文主要是介绍代码随想录 Leetcode454. 四数相加 II,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
题目:
代码 (首刷看解析 2024年1月15日):
class Solution {
public:int fourSumCount(vector<int>& nums1, vector<int>& nums2, vector<int>& nums3, vector<int>& nums4) {int n = nums1.size();unordered_map<int,int> hash;for(int a : nums1) {for(int b : nums2) {hash[a+b]++;}}int count = 0;for(int c : nums3) {for(int d : nums4) {int target = 0 - (c + d);if(hash.find(target) != hash.end()) count += hash[target];}}return count;}
};
想知道这题有无比O(n*2)更快的算法,而且这种解法真不是暴力解法吗?
这篇关于代码随想录 Leetcode454. 四数相加 II的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!