码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode "454. 4Sum II"

时间:2017-01-27 08:04:11      阅读:152      评论:0      收藏:0      [点我收藏+]

标签:etc   ima   sed   分享   hashmap   gif   ges   class   play   

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"

标签:etc   ima   sed   分享   hashmap   gif   ges   class   play   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!