/
githubmirror
/
interviews
Обзор
Документация
Войти
/
githubmirror
/
interviews
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
leetcode/array/ContainsDuplicatesII.java
18 строк
676 B
Kevin Naughton Jr
add more problem solutions
30 мар 2018, 23:29
30 мар 2018, 23:29
10347c6
Код
Авторство
О чём код?
//Given an array of integers and an integer k, find out whether there are two distinct indices i and //j in the array such that nums[i] = nums[j] and the absolute difference between i and j is at most k. class ContainsDuplicatesII { public boolean containsNearbyDuplicate(int[] nums, int k) { HashMap<Integer, Integer> map = new HashMap<Integer, Integer>(); for(int i = 0; i < nums.length; i++) { int current = nums[i]; if(map.containsKey(current) && i - map.get(current) <= k) { return true; } else { map.put(current, i); } } return false; } }