LeetCode Two Sum III - Data structure design
原题链接在这里:https://leetcode.com/problems/two-sum-iii-data-structure-design/
题目:
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.
For example,
add(1); add(3); add(5); find(4) -> true find(7) -> false
题解:
与Two Sum类似.
设计题目考虑trade off. 如果add 很多, find很少可以采用map方法.
简历HashMap保存key 和 key的frequency. find value时把HashMap的每一个key当成num1, 来查找HashMap是否有另一个value-num1的key.
num1 = value - num1时,检查num1的frequency是否大于1.
Time Complexity: add O(1). find O(n). Space: O(n).
AC Java:
1 class TwoSum { 2 3 HashMap<Integer, Integer> hm; 4 5 /** Initialize your data structure here. */ 6 public TwoSum() { 7 hm = new HashMap<Integer, Integer>(); 8 } 9 10 /** Add the number to an internal data structure.. */ 11 public void add(int number) { 12 hm.put(number, hm.getOrDefault(number, 0) + 1); 13 } 14 15 /** Find if there exists any pair of numbers which sum is equal to the value. */ 16 public boolean find(int value) { 17 for(Map.Entry<Integer, Integer> entry : hm.entrySet()){ 18 int num1 = entry.getKey(); 19 int num2 = value-num1; 20 if(hm.containsKey(num2) && (hm.get(num2)>1 || num1!=num2)){ 21 return true; 22 } 23 } 24 return false; 25 } 26 } 27 28 /** 29 * Your TwoSum object will be instantiated and called as such: 30 * TwoSum obj = new TwoSum(); 31 * obj.add(number); 32 * boolean param_2 = obj.find(value); 33 */
考虑到trade off, 上面用HashMap时add用O(1), find用O(n).
用两个Set分别存现有nums 和现有sums可以treade off.
这种设计适合少量add, 多量find操作.
Time Complexity: add O(n). find O(1). Space: O(n).
AC Java:
1 public class TwoSum { 2 HashSet<Integer> nums; 3 HashSet<Integer> sums; 4 5 /** Initialize your data structure here. */ 6 public TwoSum() { 7 nums = new HashSet<Integer>(); 8 sums = new HashSet<Integer>(); 9 } 10 11 /** Add the number to an internal data structure.. */ 12 public void add(int number) { 13 Iterator<Integer> it = nums.iterator(); 14 while(it.hasNext()){ 15 sums.add(it.next()+number); 16 } 17 nums.add(number); 18 } 19 20 /** Find if there exists any pair of numbers which sum is equal to the value. */ 21 public boolean find(int value) { 22 return sums.contains(value); 23 } 24 } 25 26 /** 27 * Your TwoSum object will be instantiated and called as such: 28 * TwoSum obj = new TwoSum(); 29 * obj.add(number); 30 * boolean param_2 = obj.find(value); 31 */
转载于:https://www.cnblogs.com/Dylan-Java-NYC/p/5324746.html
LeetCode Two Sum III - Data structure design相关推荐
- 170. Two Sum III - Data structure design【easy】
170. Two Sum III - Data structure design[easy] Design and implement a TwoSum class. It should suppor ...
- leetcode 211. Add and Search Word - Data structure design Trie树
题目链接 写一个数据结构, 支持两种操作. 加入一个字符串, 查找一个字符串是否存在.查找的时候, '.'可以代表任意一个字符. 显然是Trie树, 添加就是正常的添加, 查找的时候只要dfs查找就可 ...
- leetcode Add and Search Word - Data structure design
我要在这里装个逼啦 class WordDictionary(object):def __init__(self):"""initialize your data str ...
- LeetCode Add and Search Word - Data structure design(字典树)
问题:设计一个支持addWord,search查询的数据结构,要求search支持.正则查询 思路:使用Trie数据结构,在匹配.时,从子结点中选取可行的一个继续匹配下一个字符,主要思路是基于递归 具 ...
- LeetCode Path Sum III(前缀和)
问题: 给定一个二叉树,它的每个结点都存放着一个整数值. 找出路径和等于给定数值的路径总数. 路径不需要从根节点开始,也不需要在叶子节点结束,但是路径方向必须是向下的(只能从父节点到子节点). 二叉树 ...
- LeetCode刷题记录13——705. Design HashSet(easy)
LeetCode刷题记录13--705. Design HashSet(easy) 目录 LeetCode刷题记录13--705. Design HashSet(easy) 前言 题目 语言 思路 源 ...
- 【HDU - 4217 】Data Structure? (线段树求第k小数)
题干: Data structure is one of the basic skills for Computer Science students, which is a particular w ...
- Data Structure
Data Structure 1. Abstract Data Type (ADT) 1.1. Data type A set of objects + a set of operations Exa ...
- CF data structure 自制题单(一)
CF data structure 2000~2100 为你的战斗,献上雷鸣般的喝彩!--唔姆 目标 30 道题 1. Problem - 1555E - Codeforces 看了提示 给一些线段, ...
最新文章
- ASP.NET的Cookie跨域问题
- 文巾解题 LCP 07. 传递信息
- Ubuntu 16.04安装Chrome浏览器
- mysql多张表join_mysql 连接查询(多表查询)+子查询-初学教程 3【重点】
- python绘制不带颜色曲线图_绘制一条曲线,并根据每个点的值和定制的颜色图对其进行着色...
- 2gt;MSVCRTD.lib(MSVCR100D.dll) : error LNK2005: _calloc 已经在 LIBCMTD.lib(dbgcalloc.obj) 中定义...
- 主成分分析和因子分析_简单易懂!一文理清主成分分析思路
- u盘如何安装2003服务器系统安装,u盘怎么安装win server2003系统是iso
- java 读取xps_java读取带格式word内容
- 转帖:网络知识100问
- Python 转义字符与原字符
- CF100015B - Ball Painting
- 中国Top20天使投资机构和中国Top20VC投资机构
- QT中使用GDAL多线程读取遥感图像到QImage
- macOS中iOS模拟卡顿 ios simulator 高CPU占用解决办法
- 李宏毅2022机器学习hw6
- Excel日期修改为统一月份
- 通货膨胀与失业之间的短期权衡取舍 - 异想天开
- 关于举办“实验室生物安全”和“ISO15189医学实验室认可内审员及医学实验室质量控制”培训班的通知...
- 职场中生存的头等大事:保持尊重