Created
March 25, 2018 02:15
-
-
Save v0y4g3r/19e710d526ec319cd087ac9f592cd446 to your computer and use it in GitHub Desktop.
头条笔试题1-差值为k的数字对去重
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Arrays; | |
import java.util.BitSet; | |
import java.util.Scanner; | |
public class Main { | |
static int h(int[] nums, int dif) { | |
BitSet b = new BitSet(); | |
for (int i : nums) { | |
b.set(i); | |
} | |
Arrays.sort(nums); | |
int cnt = 0; | |
int last = nums[0]; | |
for (int i : nums) { | |
if (i != last && b.get(i + dif)) { | |
cnt++; | |
} | |
last = i; | |
} | |
if (b.get(nums[0] + dif)) cnt++; | |
return cnt; | |
} | |
public static void main(String... args) { | |
Scanner sc = new Scanner(System.in); | |
while (sc.hasNext()) { | |
int size = sc.nextInt(); | |
int dif = sc.nextInt(); | |
int[] nums = new int[size]; | |
for (int i = 0; i < size; i++) { | |
nums[i] = sc.nextInt(); | |
} | |
int res = h(nums, dif); | |
System.out.println(res); | |
} | |
sc.close(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment