查找表类算法//四数相加 II

给定四个包含整数的数组列表 A , B , C , D ,计算有多少个元组 (i, j, k, l) ,使得 A[i] + B[j] + C[k] + D[l] = 0

为了使问题简单化,所有的 A, B, C, D 具有相同的长度 N,且 0 ≤ N ≤ 500 。所有整数的范围在 -228 到 228 - 1 之间,最终结果不会超过 231 - 1 。

例如:

输入:
A = [ 1, 2]
B = [-2,-1]
C = [-1, 2]
D = [ 0, 2]

输出:
2

解释:
两个元组如下:
1. (0, 0, 0, 1) -> A[0] + B[0] + C[0] + D[1] = 1 + (-2) + (-1) + 2 = 0
2. (1, 1, 0, 0) -> A[1] + B[1] + C[0] + D[0] = 2 + (-1) + (-1) + 0 = 0
   class Solution {
        public int fourSumCount(int[] A, int[] B, int[] C, int[] D) {
            Map<Integer,Integer> map = new HashMap<Integer,Integer>();
            int count = 0;
            for(int i=0;i<A.length;i++){
                for(int j=0;j<B.length;j++){
                    if(map.containsKey(A[i]+B[j]))
                        map.put(A[i]+B[j],map.get(A[i]+B[j])+1);
                    else
                        map.put(A[i]+B[j],1);
                }
            }
            
            for(int i = 0; i < C.length; i++){
                for(int j = 0; j < D.length; j++){
                         if(map.containsKey(-C[i]-D[j]))
                             count+=map.get(-C[i]-D[j]);
                }
            }
            return count;
        }
    }

 

 class Solution {
        public int fourSumCount(int[] A, int[] B, int[] C, int[] D) {
            HashMap<Integer,Integer> map = new HashMap<>();

            int count = 0;
            for(int i=0;i<A.length;i++){
                for(int j=0;j<B.length;j++){
                    map.put(A[i]+B[j],map.getOrDefault(A[i]+B[j],0)+1);
                }
            }

            for(int i=0;i<C.length;i++){
                for(int j=0;j<D.length;j++){
                    count+= map.getOrDefault(-C[i]-D[j],0);
                }
            }
            return count;
        }
  }
class Solution {
public:
    int fourSumCount(vector<int>& A, vector<int>& B, vector<int>& C, vector<int>& D) {
        unordered_map<int,int> record;
        for(int i = 0; i < C.size(); i++){
            for(int j = 0; j < D.size(); j++){
                record[C[i]+D[j]]++;
            }
        }
        int res = 0;
        for(int i = 0; i < A.size(); i++){
            for(int j = 0; j < B.size(); j++){
                if(record.find(0-A[i]-B[j])!=record.end()){
                    res += record[0-A[i]-B[j]];
                }
            }
        }
        return res;
    }
};

 

posted @ 2018-11-08 15:17  strawqqhat  阅读(90)  评论(0编辑  收藏  举报
#home h1{ font-size:45px; } body{ background-image: url("放你的背景图链接"); background-position: initial; background-size: cover; background-repeat: no-repeat; background-attachment: fixed; background-origin: initial; background-clip: initial; height:100%; width:100%; } #home{ opacity:0.7; } .wall{ position: fixed; top: 0; left: 0; bottom: 0; right: 0; } div#midground{ background: url("https://i.postimg.cc/PP5GtGtM/midground.png"); z-index: -1; -webkit-animation: cc 200s linear infinite; -moz-animation: cc 200s linear infinite; -o-animation: cc 200s linear infinite; animation: cc 200s linear infinite; } div#foreground{ background: url("https://i.postimg.cc/z3jZZD1B/foreground.png"); z-index: -2; -webkit-animation: cc 253s linear infinite; -o-animation: cc 253s linear infinite; -moz-animation: cc 253s linear infinite; animation: cc 253s linear infinite; } div#top{ background: url("https://i.postimg.cc/PP5GtGtM/midground.png"); z-index: -4; -webkit-animation: da 200s linear infinite; -o-animation: da 200s linear infinite; animation: da 200s linear infinite; } @-webkit-keyframes cc { from{ background-position: 0 0; transform: translateY(10px); } to{ background-position: 600% 0; } } @-o-keyframes cc { from{ background-position: 0 0; transform: translateY(10px); } to{ background-position: 600% 0; } } @-moz-keyframes cc { from{ background-position: 0 0; transform: translateY(10px); } to{ background-position: 600% 0; } } @keyframes cc { 0%{ background-position: 0 0; } 100%{ background-position: 600% 0; } } @keyframes da { 0%{ background-position: 0 0; } 100%{ background-position: 0 600%; } } @-webkit-keyframes da { 0%{ background-position: 0 0; } 100%{ background-position: 0 600%; } } @-moz-keyframes da { 0%{ background-position: 0 0; } 100%{ background-position: 0 600%; } } @-ms-keyframes da { 0%{ background-position: 0 0; } 100%{ background-position: 0 600%; } }