/
githubmirror
/
interviews
Обзор
Документация
Войти
/
githubmirror
/
interviews
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
leetcode/hash-table/SingleNumberII.java
27 строк
780 B
Kevin Naughton Jr
add more problem solutions
30 мар 2018, 23:29
30 мар 2018, 23:29
10347c6
Код
Авторство
О чём код?
//Given an array of integers, every element appears three times except for one, //which appears exactly once. Find that single one. //Note: //Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? class SingleNumberII { public int singleNumber(int[] nums) { HashMap<Integer, Integer> map = new HashMap<Integer, Integer>(); for(int i: nums) { if(map.containsKey(i)) { map.put(i, map.get(i) + 1); } else { map.put(i, 1); } } for(int key: map.keySet()) { if(map.get(key) == 1) { return key; } } //no unique integer in nums return -1; } }