zoukankan      html  css  js  c++  java
  • 454. 4Sum II

    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

    此题本来以为是按照four sum的思路来做的,即先将四个数组排好序,然后两个for循环分别循环A,B数组,再用两个指针分别指向C的low和D的high,但是发现得到的数字比期待的数字少,测试用例如下:

    Input:[0,1,-1] [-1,1,0] [0,0,1] [-1,1,1]

    Output:12

    Expected:17

    后来思考了一下,发现这么做确实不可以,后两个数组每次都不能遍历完所有可能结果,由此可以推断出来,两个数组里面想用two pointer来全部遍历是无法做到的,除非刚好一个数组的最大值等于或者小于另一个数组的最小值。
    代码如下:

    public class Solution {

        public int fourSumCount(int[] A, int[] B, int[] C, int[] D) {

            int n =A.length;

            Arrays.sort(A);

            Arrays.sort(B);

            Arrays.sort(C);

            Arrays.sort(D);

            int count = 0;

            for(int i=0;i<n;i++){

                for(int j=0;j<n;j++){

                    int low = 0,high = n-1;

                    while(low<n&&high>=0){

                        int sum = A[i]+B[j]+C[low]+D[high];

                        if(sum==0){

                            count++;

                            low++;

                        }else if(sum<0){

                            low++;

                        }else{

                            high--;

                        }

                    }

                }

            }

            return count;

        }

    }

    看了大神的做法,用了hashmap来做,key保存两个数组的和,value保存出现该和的个数,然后如果另外两个数组的和等于这个hashmap的-key,就加上value的值。

    public class Solution {

        public int fourSumCount(int[] A, int[] B, int[] C, int[] D) {

            Map<Integer,Integer> map = new HashMap<Integer,Integer>();

            int n = A.length;

            for(int i=0;i<n;i++){

                for(int j=0;j<n;j++){

                    map.put(A[i]+B[j],map.getOrDefault(A[i]+B[j],0)+1);

                }

            }

            int count = 0;

            for(int i=0;i<n;i++){

                for(int j=0;j<n;j++){

                    count+=map.getOrDefault(-C[i]-D[j],0);

                }

            }

            return count;

        }

    }

    这里面Object getOrDefault(Object key, Object defaultValue),表示返回map键对应的map值,如果不存在map键,返回默认值。

  • 相关阅读:
    (转)ab(apachebench)测试与loadrunner
    hibernate学习总结
    Oracle 11G在用EXP 导入、导出时,若有空表对导入导出中遇到的问题的解决
    Nginx可以做什么
    Oracle的表空间、用户和表的区别和联系
    oracle11g 导出表报EXP-00011:table不存在。
    tomcat、nginx、apache、tengine都是什么,及其作用
    注解和依赖注入框架
    js中innerHTML与innerText的用法与区别
    Linux中 /boot 目录介绍
  • 原文地址:https://www.cnblogs.com/codeskiller/p/6354107.html
Copyright © 2011-2022 走看看