`

Leetcode - Two Sum III - Data Structure Design

    博客分类:
  • Hash
 
阅读更多
[分析]
find的version1 版本注意num2Occur 需定义为Integer类型,建议version2版本

[ref]
https://leetcode.com/discuss/19515/my-solutions-java-and-python-time-for-add-time-for-find-space

public class TwoSum {
    private HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
	public void add(int number) {
	    map.put(number, map.containsKey(number) ? 2 : 1);
	}
	// version 2
    public boolean find(int value) {
	    for (Integer num1 : map.keySet()) {
	        int num2 = value - num1;
	        if (map.containsKey(num2) && (map.get(num2) == 2 || num1 != num2))
	            return true;
	    }
	    return false;
	}
	
    // version 1
	public boolean find1(int value) {
	    for (Integer num1 : map.keySet()) {
	        Integer num2Occur = map.get(value - num1);
	        if (num2Occur != null && (num2Occur == 2 || num1 != (value - num1)))
	            return true;
	    }
	    return false;
	}
}

// Version 1: TLE
// public class TwoSum {
//     private HashSet<Integer> sumSet;
//     private HashSet<Integer> numSet;
//     public TwoSum() {
//         sumSet = new HashSet<Integer>();
//         numSet = new HashSet<Integer>();
//     }
// 	public void add(int number) {
// 	    if (!numSet.isEmpty()) {
// 	        for (int item : numSet)
// 	            sumSet.add(item + number);
// 	    }
// 	    numSet.add(number);
// 	}

// 	public boolean find(int value) {
// 	    return sumSet.contains(value);
// 	}
// }
分享到:
评论

相关推荐

    python-leetcode题解之170-Two-Sum-III-Data-structure-design.py

    针对LeetCode第170题“Two Sum III - Data structure design”,我们首先要掌握的是如何设计一个数据结构来高效地解决两数之和问题。LeetCode 170题要求设计一个数据结构,支持以下操作: 1. add(number):向数据...

    js-leetcode题解之170-two-sum-iii-data-structure-design.js

    在JavaScript中解决LeetCode的170号问题——两数之和III数据结构设计是一项涉及算法和数据结构的编程挑战。题目的核心目标是设计一个数据结构,用于存储数组中的数,并且能够判断是否存在两个不同的数,使得它们的和...

    LeetCode题解 - Java语言实现-181页.pdf

    13. Two Sum III Data structure design 两数之和III是一个变体的问题,要求设计一个数据结构来解决两数之和问题。可以使用哈希表或树形结构来解决该问题。 ... 资源摘要信息共包含181个LeetCode题解,涵盖了数组...

    LeetCode最全代码

    * [Data Structure](https://github.com/kamyu104/LeetCode#data-structure) * [Math](https://github.com/kamyu104/LeetCode#math) * [Two Pointers](https://github.com/kamyu104/LeetCode#two-pointers) * [Sort]...

    Coding Interview In Java

    15 Two Sum III Data structure design 51 16 3Sum 53 17 4Sum 55 18 3Sum Closest 57 19 String to Integer (atoi) 59 20 Merge Sorted Array 61 ... ... 231 Counting Bits 561 232 Maximum Product of Word ...

    leetcodepushfront-leetcode:leetcode问题

    leetcode 推前当前问题 5 从 9 月 9 日到 12 月 9 日,按类型。 100 个主题和 Google。 力码 Leetcode 题目汇总 分类 1、求和问题 1.1 (1) Two Sum 1.2 (15) 3 Sum 1.3 (18) 4 Sum 1.4 (454) 4 Sum II 1.5 (167) Two...

    Leetcode book刷题必备

    ***o Sum III – Data Structure Design:设计一种数据结构,支持添加和查询两数之和。 4. Valid Palindrome:判断一个字符串是否是回文,忽略大小写和非字母数字字符。 5. Implement strStr():实现字符串查找功能...

    常见算法题答案及解析

    ***o Sum III – Data Structure Design:此问题需要设计数据结构以快速判断是否存在两数和为特定值。 4. Valid Palindrome:判断一个字符串是否为回文字符串,涉及到字符串的遍历和比较。 5. Implement strStr():...

Global site tag (gtag.js) - Google Analytics