leetcode-454-四数相加 II-java

题目及测试

package pid454;
/*四数相加 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


*/


public class main {
	
	public static void main(String[] args) {
		int[][] testTable = {{1,2}};
		int[][] testTable1 = {{-2,-1}};
		int[][] testTable2 = {{-1,2}};
		int[][] testTable3 = {{0,2}};
		
			test(testTable[0],testTable1[0],testTable2[0],testTable3[0]);
		
	}
		 
	private static void test(int[] ito1,int[] ito2,int[] ito3,int[] ito4) {
		Solution solution = new Solution();
		int rtn;
		long begin = System.currentTimeMillis();
		for (int i = 0; i < ito1.length; i++) {
		    System.out.print(ito1[i]+" ");
		}//开始时打印数组
		System.out.println();
		for (int i = 0; i < ito2.length; i++) {
		    System.out.print(ito2[i]+" ");
		}//开始时打印数组
		System.out.println();
		for (int i = 0; i < ito3.length; i++) {
		    System.out.print(ito3[i]+" ");
		}//开始时打印数组
		System.out.println();
		for (int i = 0; i < ito4.length; i++) {
		    System.out.print(ito4[i]+" ");
		}//开始时打印数组
		System.out.println();
		rtn = solution.fourSumCount(ito1,ito2,ito3,ito4);//执行程序
		long end = System.currentTimeMillis();		
		System.out.println("rtn="+rtn );		
		System.out.println();
		System.out.println("耗时:" + (end - begin) + "ms");
		System.out.println("-------------------");
	}

}

没想出来

解法1(别人的)

题目说0<=N<=500
1.如果采用暴力解法O(n^4)
500^4=62500000000 这样计算机承受不了
2.利用查找表将D放入查找表中 遍历A、B、C在查找表中找是否存在-A、-B、-C
这样的话时间复杂度为O(n^3)
500^3=125000000还是太大了
3.如果能将其化解为O(n^2)的算法
500^2=250000这样是可以接受的
故只需要将C+D的每一种可能放入查找表(map)
这样我们只需要寻找这个表里面有没有-A-B就行了
有的话则A+B+C+D=0,然后统计一下个数

将数组C,D 任意组合的和存入查找表中, key是和,value 是出现的次数。记录A,B 任意组合的和的负值,然后在查找表中查找是否有对应的值
时间复杂度:O(n^2)。

class Solution {
    public int fourSumCount(int[] A, int[] B, int[] C, int[] D) {
        HashMap<Integer,Integer> map=new HashMap<>();
        
        int sum=0;
        for(int i=0;i<A.length;i++){
            for(int j=0;j<B.length;j++){
                sum=A[i]+B[j];
                if(map.containsKey(sum)){
                    map.put(sum,map.get(sum)+1);
                }else{
                    map.put(sum,1);
                }
                
            }
        }
        
        int count=0;
        int sum2=0;
        for(int i=0;i<C.length;i++){
            for(int j=0;j<D.length;j++){
                sum2=-C[i]-D[j];
                if(map.containsKey(sum2)){
                    count+=map.get(sum2);
                }
                
                
            }
        }
        return count;
            
    }
}

解法2(成功,170ms,很慢)

public class Solution {
	public int fourSumCount(int[] A, int[] B, int[] C, int[] D) {
		if(A.length == 0 || B.length == 0 || C.length == 0 || D.length == 0 ) {
			return 0;
		}
		HashMap<Integer, Integer> mapAB = new HashMap<Integer, Integer>();
		HashMap<Integer, Integer> mapCD = new HashMap<Integer, Integer>();
		for(int i=0;i<A.length;i++) {
			for(int j=0;j<B.length;j++) {
				int now = A[i] + B[j];
				if(mapAB.containsKey(now)) {
					mapAB.put(now, mapAB.get(now) + 1);
				}else {
					mapAB.put(now, 1);
				}
			}
		}
		for(int i=0;i<C.length;i++) {
			for(int j=0;j<D.length;j++) {
				int now = C[i] + D[j];
				if(mapCD.containsKey(now)) {
					mapCD.put(now, mapCD.get(now) + 1);
				}else {
					mapCD.put(now, 1);
				}
			}
		}
		int result = 0;
		for(Integer keyAB:mapAB.keySet()) {
			if(mapCD.containsKey(-keyAB)) {
				result = result + mapAB.get(keyAB) * mapCD.get(-keyAB);
			}
		}
		return result;
	}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值