Pairs(hashset的查找應用)

Given N integers, count the total pairs of integers that have a difference of K.

Input Format 
The 1st line contains N & K (integers).
The 2nd line contains N numbers of the set. All the N numbers are assured to be distinct.

Output Format
An integer that tells the number of pairs of numbers that have a difference of K.

Constraints:
N <= 105
0 < K < 109
Each integer will be greater than 0 and at least K smaller than 231-1

Sample Input #00:

5 2  
1 5 3 4 2  

Sample Output #00:

3

Sample Input #01:

10 1  
363374326 364147530 61825163 1073065718 1281246024 1399469912 428047635 491595254 879792181 1069262793 

Sample Output #01:

0
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    static int pairs(int[] a,int k) {
      /* Complete this function */
    	int count  = 0;
		HashSet<Integer> hs = new HashSet<Integer>();
    	for (int i : a) {
			hs.add(i);
		}
    	for (Integer integer : hs) {
			if(hs.contains(integer+k))
			{
				count++;
			}
		}
        return count;
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int res;
        
        String n = in.nextLine();
        String[] n_split = n.split(" ");
        
        int _a_size = Integer.parseInt(n_split[0]);
        int _k = Integer.parseInt(n_split[1]);
        
        int[] _a = new int[_a_size];
        int _a_item;
        String next = in.nextLine();
        String[] next_split = next.split(" ");
        
        for(int _a_i = 0; _a_i < _a_size; _a_i++) {
            _a_item = Integer.parseInt(next_split[_a_i]);
            _a[_a_i] = _a_item;
        }
        
        res = pairs(_a,_k);
        System.out.println(res);
    }
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章