Given four lists A, B, C, D of integer values, compute how many tuples (i, j, k, l) there are such that A[i] + B[j] + C[k] + D[l] is zero.

To make problem a bit easier, all A, B, C, D have same length of N where 0 ≤ N ≤ 500. All integers are in the range of -228 to 228 - 1 and the result is guaranteed to be at most 231 - 1.

Example:

Input:
A = [ 1, 2]
B = [-2,-1]
C = [-1, 2]
D = [ 0, 2] Output:
2 Explanation:
The two tuples are:
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

一开始想了一个O(n^3), space O(N)的做法,后来发现还可以优化

Solution: time O(N^2), space O(N^2)

 public class Solution {
public int fourSumCount(int[] A, int[] B, int[] C, int[] D) {
HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
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++) {
res += map.getOrDefault(-(C[i]+D[j]), 0);
}
}
return res;
}
}

Leetcode: 4Sum II的更多相关文章

  1. [LeetCode] 4Sum II 四数之和之二

    Given four lists A, B, C, D of integer values, compute how many tuples (i, j, k, l) there are such t ...

  2. LeetCode 454. 4Sum II

    454. 4Sum II Add to List Description Submission Solutions Total Accepted: 8398 Total Submissions: 18 ...

  3. [LeetCode] 4Sum 四数之和

    Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = tar ...

  4. leetcode Permutations II 无重全排列

    作者:jostree  转载请注明出处 http://www.cnblogs.com/jostree/p/4051169.html 题目链接:leetcode Permutations II 无重全排 ...

  5. LeetCode——4Sum &amp; 总结

    LeetCode--4Sum & 总结 前言 有人对 Leetcode 上 2Sum,3Sum,4Sum,K Sum问题作了总结: http://blog.csdn.net/nanjunxia ...

  6. 454. 4Sum II

    Given four lists A, B, C, D of integer values, compute how many tuples (i, j, k, l) there are such t ...

  7. LC 454. 4Sum II

    Given four lists A, B, C, D of integer values, compute how many tuples (i, j, k, l) there are such t ...

  8. [LeetCode] 454. 4Sum II 四数之和之二

    Given four lists A, B, C, D of integer values, compute how many tuples (i, j, k, l) there are such t ...

  9. [LeetCode] 454. 4Sum II 四数之和II

    Given four lists A, B, C, D of integer values, compute how many tuples (i, j, k, l) there are such t ...

随机推荐

  1. implicit和explicit的基本使用

    class MyAge { public int Age { get; set; } public static implicit operator MyAge(int age) { return n ...

  2. SDL1.2学习

    http://wenku.baidu.com/view/c953c0878762caaedd33d4d8.html 一.安装: sudo apt-get install libsdl1.2-dev l ...

  3. nfs的使用

    1.安装命令:sudo apt-get install nfs-kernel-server ;   sudo apt-get install nfs-common; 2.执行命令:mkdir /(目录 ...

  4. Hightcharts动态创建series

    第一种方法: 申明options时动态设置series,然后再创建chart对象 代码如下: <html> <head> <title>Highcharts Exa ...

  5. setTimeout的用法

    var n = 0; function fnTime(){alert(++n);}// 常用写法// 不加括号方式setTimeout(fnTime,5000);// 加字符串方式setTimeout ...

  6. css重新认识(2)

    1)行内元素可以用margin-left与margin-right调整位置,但用margin-top与margin-bottom来调整位置不会有效果,只有具有block属性值后才能像块级元素般被外边距 ...

  7. c# winform进入窗口后在文本框里的默认焦点

    c# winform 设置winform进入窗口后在文本框里的默认焦点   进入窗口后默认聚焦到某个文本框,两种方法: ①设置tabindex 把该文本框属性里的tabIndex设为0,焦点就默认在这 ...

  8. ASCII值对照表

    说明:这里的ascii的值是十进制的 ASCII值 控制字符 ASCII值 控制字符 ASCII值 控制字符 ASCII值 控制字符 0 NUT 32 (space) 64 @ 96 . 1 SOH ...

  9. wxpython更新

    .configure时候检查不到gtk+ 使用 apt-get install gnome-core-devel

  10. JSCH实现文件上传的代码实例

    package com.vcredit.ddcash.monitor.sendmail; import java.io.File;import java.io.FileInputStream;impo ...