/** * https://www.lintcode.com/problem/two-sum-iii-data-structure-design/description * * Design and implement a TwoSum class. * It should support the following operations: add and find. add - Add the number to an internal data structure. find - Find if there exists any pair of numbers which sum is equal to the value. Example Example 1: add(1); add(3); add(5); find(4) // return true find(7) // return false * */ class Solustion { val map = HashMap<Int, Int>() fun add(value: Int) { map.put(value, map.getOrDefault(value, 0) + 1) } fun find(needFind: Int): Boolean { for ((key,value) in map){ val diff = needFind-key if ((diff != key && map.containsKey(diff)) || (diff == key && value > 1)) { return true } } return false } }
Two Sum III - Data structure design
猜你喜欢
转载自www.cnblogs.com/johnnyzhao/p/10665109.html
今日推荐
周排行