题目链接:Pairs

完全就是Two Sum问题的变形!Two Sum问题是要求数组中和正好等于K的两个数,这个是求数组中两个数的差正好等于K的两个数。总结其实就是“骑驴找马”的问题:即当前遍历ar[i],那么只要看数组中是否存在ar[i]+K或者ar[i]-K就可以了,还是用HashMap在O(1)的时间完成这个操作。

题目有一点没说清楚的就是元素是否有重复,从Editorial来看似乎是没有重复,不过我还是用map的value记录了数出现的频率来处理了重复。

代码如下:

 import java.util.*;

 public class Solution {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int k = in.nextInt();
HashMap<Integer, Integer> map = new HashMap<Integer,Integer>();
int[] ar = new int[n];
for(int i = 0;i < n;i ++){
ar[i] = in.nextInt();
if(!map.containsKey(ar[i]))
map.put(ar[i], 0);
map.put(ar[i], map.get(ar[i])+1);
} int answer = 0;
for(int i = 0;i < n;i ++){
int up = ar[i]+k;
if(map.containsKey(up))
answer += map.get(up);
int down = ar[i]-k;
if(map.containsKey(down))
answer += map.get(down);
}
System.out.println(answer/2); }
}

【HackerRank】Pairs的更多相关文章

  1. 【HackerRank】How Many Substrings?

    https://www.hackerrank.com/challenges/how-many-substrings/problem 题解 似乎是被毒瘤澜澜放弃做T3的一道题(因为ASDFZ有很多人做过 ...

  2. 【hackerrank】Week of Code 26

    在jxzz上发现的一个做题网站,每周都有训练题,题目质量……前三题比较水,后面好神啊,而且类型差不多,这周似乎是计数专题…… Army Game 然后给出n*m,问需要多少个小红点能全部占领 解法:乘 ...

  3. 【HackerRank】Closest Numbers

    Sorting is often useful as the first step in many different tasks. The most common task is to make f ...

  4. 【HackerRank】Lonely Integer

    There are N integers in an array A. All but one integer occur in pairs. Your task is to find out the ...

  5. 【HackerRank】Running Time of Quicksort

    题目链接:Running Time of Quicksort Challenge In practice, how much faster is Quicksort (in-place) than I ...

  6. 【hackerrank】Week of Code 30

    Candy Replenishing Robot Find the Minimum Number 直接模拟 Melodious password dfs输出方案 Poles 题意:有多个仓库,只能从后 ...

  7. 【HackerRank】Median

    题目链接:Median 做了整整一天T_T 尝试了各种方法: 首先看了解答,可以用multiset,但是发现java不支持: 然后想起来用堆,这个基本思想其实很巧妙的,就是维护一个最大堆和最小堆,最大 ...

  8. 【HackerRank】Coin on the Table

    题目链接:Coin on the Table 一开始想用DFS做的,做了好久都超时. 看了题解才明白要用动态规划. 设置一个三维数组dp,其中dp[i][j][k]表示在时间k到达(i,j)所需要做的 ...

  9. 【HackerRank】Cut the tree

    题目链接:Cut the tree 题解:题目要求求一条边,去掉这条边后得到的两棵树的节点和差的绝对值最小. 暴力求解会超时. 如果我们可以求出以每个节点为根的子树的节点之和,那么当我们去掉一条边(a ...

随机推荐

  1. nagios 安装

    #!/bin/sh ################################################ #this scripts is created by oldboy #site: ...

  2. python之斐波那契数列

    斐波那契数列(Fibonacci sequence),又称黄金分割数 这样的一个数列:0,1,1,2,3,8,13,21,34…….. 特别指出:0不是第一项,而是第零项. 在数学上被以 递归的方法定 ...

  3. Bootstrap -- 标签属性

    @.aria-label和aria-labelledby 用于屏幕阅读器 原文:https://blog.csdn.net/liuyan19891230/article/details/5045283 ...

  4. Servlet 环境设置

    开发环境是您可以开发.测试.运行 Servlet 的地方. 就像任何其他的 Java 程序,您需要通过使用 Java 编译器 javac 编译 Servlet,在编译 Servlet 应用程序后,将它 ...

  5. spark hive结合杂记(hive-site.xml)

    1.下载spark源码,在spark源码目录下面有个make-distribution.sh文件,修改里面的参数,使编译后能支持hive,修改后执行该文件.(要预先安装好maven才能编译). 2.将 ...

  6. 【转发】linux开发人员常用命令

    每个开发人员到了他们职业人生的某个阶段的时候,将会发现自己要寻找有关Linux的信息.我并不是这方面的专家.但是掌握了以下8个命令,我几乎可以得到我任何需要的东西. 注意:以下的命令都有很多扩展的文档 ...

  7. ios -完全实现代码设置 Could not find a storyboard named 'Main' in bundle NSBundle

    UIWindow *windows = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds]; windows.background ...

  8. 使用神器MobaXterm连接远程mysql和redis

    https://mobaxterm.mobatek.net/download-home-edition.html mysql redis 连接测试 mysql 127.0.0.1 3307 密码使用线 ...

  9. 安装使用yarn,使用国内镜像加速npm和yarn

    安装yarn https://yarnpkg.com/lang/zh-hans/docs/install/ 使用国内镜像加速npm和yarn 1. npm config set registry=ht ...

  10. C# Static修饰符的作用

    MSDN上的定义 Use the static modifier to declare a static member, which belongs to the type itself rather ...