整数对查找

时间最优的解法

时间复杂度为O(N)
空间复杂度为O(N)

package 整数对查找;

import java.util.HashMap;

/**
 * Created by h on 16-12-17.
 */
public class Solution {
    public int countPairs(int[] A, int n, int sum) {
        // write code here
        HashMap<Integer, Integer> hms = new HashMap<Integer, Integer>();
        HashMap<Integer, Boolean> visited = new HashMap<Integer, Boolean>();
        for (int i :
                A) {
            if (hms.containsKey(i)) {
                hms.put(i, hms.get(i)+1);
            } else {
                hms.put(i, 1);
                visited.put(i, false);
            }
        }
        int ans = 0;
        for (Integer i:
             hms.keySet()) {
            if(!visited.get(i)) {
                int another = sum - i;
                if (another == i) {
                    int num = hms.get(i);
                    ans += (num - 1) * num / 2;
                } else {
                    if (hms.containsKey(another)) {
                        ans += hms.get(i) * hms.get(another);
                        visited.put(another,true);
                    } else {
                        continue;
                    }
                }
            }
            visited.put(i, true);
        }
        return ans;
    }

    public static void main(String[] args) {
        int[] A = {11,7,7,6,14,2,14,15,2,1,2,12,13,9,8,15,13,8,10,11,14,10,2,9,4,9,3,7,6,10,15,4,7,6,15,3,9,13,5,2,6,10,10,1,12,4,3,3,8,8,1,4,7,11,13,5,13,15,4,3,1,11,6,11,9,9,11,15,12,10,13,3,11,4,8,9,7,3,13,9,11,3,2,11,10,1,4,2,3,3,14,11,5,10,1,14,8,1,11,3,1,9,14,6,1,7,15,10,14,6,4,12,11};
        int n = A.length;
        int sum = 16;
        System.out.println(new Solution().countPairs(A, n, sum));
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值