Two Sum III - Data structure design LT170
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
Idea 1. Similar to Two Sum LT1, if numbers are unique, set would be enough, otherwise map to store frequency for each number. 遍历hashmap, 对于每个数num,找target - num,
record[num] >= 2 if target - num = num
record[target - num] >= 1 if target - num != num
Time complexity: O(1) for add, O(n) for find, or alternatively add set to store the sum, so that O(n) for add, O(1) for find
Space complexity: O(n)
public class TwoSum {
private Map<Integer, Integer> record = new HashMap<>();
public void add(int number) {
record.put(number, record.getOrDefault(number, 0) + 1);
}
public boolean find(int value) {
for(int key: record.keySet()) {
int another = value - key;
if(key == another) {
if(record.get(another) >= 2) {
return true;
}
}
else {
if(record.containsKey(another)){
return true;
}
}
}
return false;
}
public static void main(String[] args) {
TwoSum subject = new TwoSum();
subject.add(1);
subject.add(3);
subject.add(4);
System.out.println(subject.find(4));
System.out.println(subject.find(7));
}
}
slightly more conciser:
public class TwoSum {
private Map<Integer, Integer> record = new HashMap<>();
public void add(int number) {
record.put(number, record.getOrDefault(number, 0) + 1);
}
public boolean find(int value) {
for(int key: record.keySet()) {
int another = value - key;
if((key == another && record.get(another) >= 2)
|| (key != another && record.containsKey(another))){
return true;
}
}
return false;
}
}
Two Sum III - Data structure design LT170的更多相关文章
- 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] Two Sum III - Data structure design 两数之和之三 - 数据结构设计
Design and implement a TwoSum class. It should support the following operations:add and find. add - ...
- ✡ leetcode 170. Two Sum III - Data structure design 设计two sum模式 --------- java
Design and implement a TwoSum class. It should support the following operations: add and find. add - ...
- LeetCode Two Sum III - Data structure design
原题链接在这里:https://leetcode.com/problems/two-sum-iii-data-structure-design/ 题目: Design and implement a ...
- LeetCode 笔记27 Two Sum III - Data structure design
Design and implement a TwoSum class. It should support the following operations: add and find. add - ...
- leetcode3 Two Sum III – Data structure design
Question: Design and implement a TwoSum class. It should support the following operations: add and f ...
- 170. Two Sum III - Data structure design
题目: Design and implement a TwoSum class. It should support the following operations: add and find. a ...
- leetcode[170]Two Sum III - Data structure design
Design and implement a TwoSum class. It should support the following operations: add and find. add - ...
- LeetCode 170. Two Sum III - Data structure design (两数之和之三 - 数据结构设计)$
Design and implement a TwoSum class. It should support the following operations: add and find. add - ...
随机推荐
- Mybatis多个in查询
Map<String, Object> params = null; List<Map<String, Object>> list=new ArrayList(); ...
- Laravel5.1 与 Laypage 结合进行分页
demo地址:http://lara.ytlwin.top/orm 路由 Route::match(array('get','post'),'/orm','StuController@orm'); 控 ...
- SIGTERM、SIGKILL、SIGINT和SIGQUIT的区别
转自:http://blog.csdn.net/dai_xiangjun/article/details/41871647 SIGQUIT 在POSIX兼容的平台,SIGQUIT是其控制终端发送到进程 ...
- Django项目的创建与管理和pycharm与Github的秘密
随笔 - 174 文章 - 21 评论 - 19 Django项目创建与管理 1.主题 这部分教程主要介绍如何通过Pycharm创建.管理.运行一个Django工程.对于Django模块的相关 ...
- kafka集群压力测试--基础。
1.生产者测试 kafka-producer-perf-test.bat --num-records 1000000 --topic test --record-size 200 --throughp ...
- 一个范围的两个数进行数位的累加,实现对两个数num1和num2的数位和相加
对一个范围的两个数进行数位的累加,例如有两个数 15,19则 他们的数位和应该为: 1+5+1+6+1+7+1+8+1+9,结果为40. 测试说明 样例1 输入:1519 输出: 40 以下是不同方法 ...
- win8 IIS配置
iis发展到win8已经到是iis8.0了.至于如何安装的,咱们接下来看.大体和iis6安装差别不大.iis8安装起来也是很方便的. 1.打开“控制面板”——“程序和功能”.如下图所示 2.点击“启用 ...
- [leetcode]151. Reverse Words in a String翻转给定字符串中的单词
Given an input string, reverse the string word by word. Example: Input: "the sky is blue", ...
- web中CookieUtils的工具类
该类中包含Web开发中对Cookie的常用操作,如需要Copy带走 package com.project.utils; import java.io.UnsupportedEncodingExcep ...
- List<Map<String, String>>和Map<String, List<String>>遍历
public void TestM() { List<Map<String, String>> lm = new ArrayList<>(); Ma ...