zoukankan      html  css  js  c++  java
  • 4sum-ii

    https://leetcode.com/problems/4sum-ii/

    用了个两两匹配的方法,还是不错的

    package com.company;
    
    
    import java.util.HashMap;
    import java.util.Map;
    
    class Solution {
        public int fourSumCount(int[] A, int[] B, int[] C, int[] D) {
            Map<Integer, Integer> mp1 = new HashMap<>();
            int len = A.length;
            int sum = 0;
    
            for (int i=0; i<len; i++) {
                for (int j=0; j<len; j++) {
                    sum = A[i] + B[j];
                    if (mp1.containsKey(sum)) {
                        mp1.put(sum, mp1.get(sum)+1);
                    }
                    else {
                        mp1.put(sum, 1);
                    }
                }
            }
    
            int ret = 0;
            for (int i=0; i<len; i++) {
                for (int j=0; j<len; j++) {
                    sum = 0 - C[i] - D[j];
                    if (mp1.containsKey(sum)) {
                        ret += mp1.get(sum);
                    }
                }
            }
            return ret;
    
        }
    }
    
    public class Main {
    
        public static void main(String[] args) throws InterruptedException {
    
            System.out.println("Hello!");
            Solution solution = new Solution();
    
            // Your Codec object will be instantiated and called as such:
            int[] A = {1, 2};
            int[] B = {-2, -1};
            int[] C = {-1, 2};
            int[] D = {0, 2};
            int ret = solution.fourSumCount(A, B, C, D);
            System.out.printf("ret:%d
    ", ret);
    
            System.out.println();
    
        }
    
    }
  • 相关阅读:
    01-节点层次
    WebAPI02
    WebAPI01
    牛客剑指Offer7
    Python字典排序
    Python字典中的键映射多个值
    计算机硬件的主要技术指标
    计算机的基本组成
    计算机系统概论
    数据库概论
  • 原文地址:https://www.cnblogs.com/charlesblc/p/6074896.html
Copyright © 2011-2022 走看看