资讯专栏INFORMATION COLUMN

[LeetCode] 170. Two Sum III - Data structure desig

dack / 612人阅读

Problem

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 1:

add(1); add(3); add(5);
find(4) -> true
find(7) -> false

Example 2:

add(3); add(1); add(2);
find(3) -> true
find(6) -> false

Solution
class TwoSum {
    Map map;
    public TwoSum() {
        map = new HashMap<>();
    }
    
    public void add(int number) {
        map.put(number, map.getOrDefault(number, 0)+1);
    }
    
    public boolean find(int value) {
        for (Map.Entry entry: map.entrySet()) {
            int i = entry.getKey();
            int j = value-i;
            if ((i == j && entry.getValue() >= 2) ||
                (i != j && map.containsKey(j))) {
                return true;
            }
        }
        return false;
    }
}
Two HashSet -- TLE
class TwoSum {

    Set nums;
    Set sums;
    /** Initialize your data structure here. */
    public TwoSum() {
        nums = new HashSet<>();
        sums = new HashSet<>();
    }
    
    /** Add the number to an internal data structure.. */
    public void add(int number) {
        Iterator iterator = nums.iterator();
        while (iterator.hasNext()) {
            sums.add(iterator.next()+number);
        }
        nums.add(number);
    }
    
    /** Find if there exists any pair of numbers which sum is equal to the value. */
    public boolean find(int value) {
        return sums.contains(value);
    }
}

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/71903.html

相关文章

  • Two Sum系列 Leetcode解题记录

    摘要:如果没有,就到里面复杂度分析就是,因为只扫了一遍数组。复杂度分析当然就是最坏情况了,也是标准的双指针复杂度。复杂度分析这种题应该不太需要分析复杂度吧,能实现就行。复杂度分析还是最后再说两句所以可以看出,很多题目思路一致,换汤不换药。 Two Sum 友情提示:篇幅较长,找题目的话,右边有目录,幸好我会MarkDown语法。改成了系列模式,因为类似的题不少,本质上都是换壳,所以在同一篇文...

    andong777 评论0 收藏0
  • LeetCode 攻略 - 2019 年 7 月下半月汇总(100 题攻略)

    摘要:月下半旬攻略道题,目前已攻略题。目前简单难度攻略已经到题,所以后面会调整自己,在刷算法与数据结构的同时,攻略中等难度的题目。 Create by jsliang on 2019-07-30 16:15:37 Recently revised in 2019-07-30 17:04:20 7 月下半旬攻略 45 道题,目前已攻略 100 题。 一 目录 不折腾的前端,和咸鱼有什么区别...

    tain335 评论0 收藏0
  • 前端 | 每天一个 LeetCode

    摘要:在线网站地址我的微信公众号完整题目列表从年月日起,每天更新一题,顺序从易到难,目前已更新个题。这是项目地址欢迎一起交流学习。 这篇文章记录我练习的 LeetCode 题目,语言 JavaScript。 在线网站:https://cattle.w3fun.com GitHub 地址:https://github.com/swpuLeo/ca...我的微信公众号: showImg(htt...

    张汉庆 评论0 收藏0
  • [Leetcode] Path Sum I & II & III 路径和1,2,3

    摘要:只要我们能够有一个以某一中间路径和为的哈希表,就可以随时判断某一节点能否和之前路径相加成为目标值。 最新更新请见:https://yanjia.me/zh/2019/01/... Path Sum I Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that addin...

    caiyongji 评论0 收藏0
  • [LeetCode] Combination Sum III | Combination Sum I

    摘要:此时,若也正好减小为,说明当前集合是正解,加入数组。两个无法得到正解的情况是在为,而不为时,当然已经无法得到正解,。在不为而却已经小于等于的情况下,此时仍要加入其它数以令为,而要加入的数都是到的正整数,所以已无法满足令为的条件,。 Combination Sum I & II: link Combination Sum III Problem Find all possible com...

    leiyi 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<