首页 > 其他 > 详细

LeetCode "454. 4Sum II"

时间:2017-01-27 08:02:45      阅读:283      评论:0      收藏:0      [点我收藏+]

Split it into 2 group of arrays, 2 arrays in each group. Then use hashmap.

技术分享
class Solution {
public:
    int fourSumCount(vector<int>& A, vector<int>& B, vector<int>& C, vector<int>& D) {
        int n = A.size(); if(!n) return 0;
        
        int ret = 0;
        unordered_map<int, unsigned> hm1, hm2;
        for(int i = 0; i < n; i ++)
        for(int j = 0; j < n; j ++)
            hm1[A[i] + B[j]] ++, hm2[C[i] + D[j]] ++;
        
        for(auto &kv: hm1)
            if(hm2.find(-kv.first) != hm2.end())
                ret += kv.second * hm2[-kv.first];
        
        return ret;
    }
};
View Code

 

LeetCode "454. 4Sum II"

原文:http://www.cnblogs.com/tonix/p/6353622.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!