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 的类。包含add和find两种操作方式。

1、使用HashMap和HashSet实现。

public class TwoSum {

    Map<Integer,Integer> map = new HashMap<Integer,Integer>();
Set<Integer> set = new HashSet<Integer>();
// Add the number to an internal data structure.
public void add(int number) {
if (set.contains(number)){
map.put(number, 2);
}else {
set.add(number);
map.put(number, 1);
}
} // Find if there exists any pair of numbers which sum is equal to the value.
public boolean find(int value) {
for (int num : set){
if (num == value - num){
if (map.get(num) == 2){
return true;
}
} else if (set.contains(value - num)){
return true;
}
}
return false;
}
} // Your TwoSum object will be instantiated and called as such:
// TwoSum twoSum = new TwoSum();
// twoSum.add(number);
// twoSum.find(value);

2、使用HashMap 和 List(也可以只使用一个HashMap,记录一或者2即可。)

public class TwoSum {

    private List<Integer> list = new ArrayList<Integer>();
private Map<Integer, Integer> map = new HashMap<Integer, Integer>(); // Add the number to an internal data structure.
public void add(int number) {
if (map.containsKey(number)) map.put(number, map.get(number) + 1);
else {
map.put(number, 1);
list.add(number);
}
} // Find if there exists any pair of numbers which sum is equal to the value.
public boolean find(int value) {
for (int i = 0; i < list.size(); i++){
int num1 = list.get(i), num2 = value - num1;
if ((num1 == num2 && map.get(num1) > 1) || (num1 != num2 && map.containsKey(num2))) return true;
}
return false;
}
} // Your TwoSum object will be instantiated and called as such:
// TwoSum twoSum = new TwoSum();
// twoSum.add(number);
// twoSum.find(value);

3、为什么要使用一个List,因为在遍历操作的时候,List要比Map或者Set快得多,使用一个List可以用空间换取时间。

✡ leetcode 170. Two Sum III - Data structure design 设计two sum模式 --------- java的更多相关文章

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

  2. LeetCode 170. Two Sum III - Data structure design (两数之和之三 - 数据结构设计)$

    Design and implement a TwoSum class. It should support the following operations: add and find. add - ...

  3. 【LeetCode】170. Two Sum III – Data structure design

    Difficulty:easy  More:[目录]LeetCode Java实现 Description Design and implement a TwoSum class. It should ...

  4. [LeetCode] 170. Two Sum III - Data structure design 两数之和之三 - 数据结构设计

    Design and implement a TwoSum class. It should support the following operations:add and find. add - ...

  5. leetcode[170]Two Sum III - Data structure design

    Design and implement a TwoSum class. It should support the following operations: add and find. add - ...

  6. [leetcode]170. Two Sum III - Data structure design两数之和III - 数据结构设计

    Design and implement a TwoSum class. It should support the following operations: add and find. add - ...

  7. 【leetcode】170. Two Sum III - Data structure design 两数之和之三 - 数据结构设计

    Design and implement a TwoSum class. It should support the following operations:  add and find. add  ...

  8. 【LeetCode】170. Two Sum III - Data structure design 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 数组+字典 平衡查找树+双指针 日期 题目地址:htt ...

  9. [LeetCode] Two Sum III - Data structure design 两数之和之三 - 数据结构设计

    Design and implement a TwoSum class. It should support the following operations:add and find. add - ...

随机推荐

  1. css 之 1.基本语法规范

    文章转自:http://www.10wy.net/Article/CSS/CSS_list_8.html查看更多更专业性的文章请到:网页设计网 第一篇 CSS 1.基本语法规范 分析一个典型CSS的语 ...

  2. TCP Socket 通讯(客户端与服务端)

    /*----------------------------编译环境:VS2015---------------------------------------*/ /*--------------- ...

  3. cookies的获取,删除,设置

    cookies,sessionStorage 和 localStorage 的区别? 1.cookie在浏览器和服务器间来回传递. sessionStorage和localStorage不会: 2.s ...

  4. python中转义用法 r''

    代码中需要转多个字符,,可以使用 r'' 例子: print(r"'''\\sfd/;fe'lsdfl")

  5. .Net生成HTML的三种方法

    一.在服务器上指定aspx网页,生成html静态页 public partial class Default2 : System.Web.UI.Page { protected void Page_L ...

  6. 关闭键盘导致tableView:didSelectRowAtIndexPath:失效解决办法

    今天公司的小兄弟问了tableView:didSelectRowAtIndexPath:不能执行的问题. 从经验看觉得可能是控制器没有成为tableView的代理所致.但代码中已经添加了代码 _tab ...

  7. Spark源码学习1.3——TaskSetManager.scala

    TaskSetManager.scala TaskSet是指一系列被提交的task,一般是代表特定的stage中丢失的partition.TaskSetManager通过一个TaskScheduler ...

  8. 算法(第4版)-1.5 案例研究:union-find算法

    问题→ 动态连通性:当程序从输入中读取了整数对p q时,如果已知的所有整数对都不能说明p和q是相连的,那么则将这一对整数写入到输出中.如果已知的数据可以说明p和q 是相连的,那么程序应该忽略p q这对 ...

  9. Orchard中的命令行工具

    在Orchard中提供了一个命令行工具,我们可以使用这个命令行工具创建用户.创建博客.生成代码.配置网站.打包模块等.并且这个命令行工具是可以扩充的,只要我们在自己开发的模块中创建一个Command类 ...

  10. iOS UITextField限制输入数字

    有时候项目中要求文本框中只能输入数字,如:价格.公里数.费用等等,一般的文本框不限制输入的格式,这时候只能强制限制输入框的输入格式了,代码如下: #import "ViewControlle ...