leetcode — two-sum-iii-data-structure-design
import java.util.HashMap;
import java.util.Map;
/**
* Source : https://oj.leetcode.com/problems/two-sum-iii-data-structure-design/
*
* Created by lverpeng on 2017/6/23.
*
*
* 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
*
*/
public class TwoSum3 {
private Map<Integer, Integer> nums = new HashMap<Integer, Integer>();
/**
* 这个题目里面的数组不再是给定的,而是输入的,而且数组元素可能是重复的,因为还是查找,所以可以用hash,
* add的数字为key,value为该可以重复出现的次数
*
* @param num
*/
public void add (int num) {
int count = 0;
if (nums.containsKey(num)) {
count = nums.get(num);
}
nums.put(num, count + 1);
}
/**
* 因为元素可能存在重复,所以要分两种情况考虑:
* 1. 正好两个相等的数的和为指定的数
* 2. 两个加数不相等,但是hash表中包含第二个加数
* 上面两种情况是能找到的,其他情况找不到指定的value
*
* @param value
* @return
*/
public boolean find (int value) {
int one = 0;
int two = 0;
for (Integer num : nums.keySet()) {
one = num;
two = value - one;
if ((one == two && nums.get(num) > 1) || (one != two && nums.containsKey(two))) {
return true;
}
}
return false;
}
public static void main(String[] args) {
TwoSum3 twoSum3 = new TwoSum3();
twoSum3.add(1);
twoSum3.add(3);
twoSum3.add(5);
System.out.println(twoSum3.find(4));
System.out.println(twoSum3.find(7));
}
}
leetcode — two-sum-iii-data-structure-design的更多相关文章
- [LeetCode] Two Sum III - Data structure design 两数之和之三 - 数据结构设计
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 ...
- 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 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
Difficulty:easy More:[目录]LeetCode Java实现 Description Design and implement a TwoSum class. It should ...
- [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 设计two sum模式 --------- java
Design and implement a TwoSum class. It should support the following operations: add and find. add - ...
- LeetCode 笔记27 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 - ...
- [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 - ...
随机推荐
- MySQL开发——【高级操作、五子句】
高级新增操作 ①基本插入操作: insert into 数据表名称(字段) values (值); ②批量插入操作: insert into 数据表名称(字段) values (值1),(值2),(值 ...
- tick 能见度
1.生成图形 当图片中的内容较多,相互遮盖时,可以通过设置相关内容的透明度来使图片更易于观察,也即是通过本节中的bbox参数设置来调节图像信息. import matplotlib.pyplot as ...
- docker 支持ipv6 (核心要点是ndp需要把docker内的ip全部加入到ndplist中来)
IPv6 with Docker Estimated reading time: 10 minutes The information in this section explains IPv6 wi ...
- Git 在团队中的使用--如何正确使用Git Flow
Git的优点 Git的优点很多,但是这里只列出我认为非常突出的几点. 由于是分布式,所有本地库包含了远程库的所有内容. 优秀的分支模型,打分支以及合并分支,机器方便. 快速,在这个时间就是金钱的时代, ...
- 20172306 2018-2019《Java程序设计与数据结构课堂测试补充报告》
学号 2017-2018-2 <程序设计与数据结构>课堂测试补充报告 课程:<程序设计与数据结构> 班级: 1723 姓名: 刘辰 学号:20172306 实验教师:王志强 必 ...
- javascript 跨域 的几种方法
1.jsonp方法 转:https://blog.csdn.net/liusaint1992/article/details/50959571 主要实现功能: 1.参数拼装. 2.给每个回调函数唯 ...
- 使用注解配置Spring
使用注解配置Spring 1.为主配置文件引入新的命名空间(约束) 2.开启使用注解代理配置文件 3.在类中使用注解完成配置 将对象注册到容器 修改对象的作用范围 值类型注入 引用类型注入 注意: 初 ...
- ABP框架系列之五十四:(XSRF-CSRF-Protection-跨站请求伪造保护)
Introduction "Cross-Site Request Forgery (CSRF) is a type of attack that occurs when a maliciou ...
- Day03(黑客成长日记)
#猜数游戏 != 是不等于 # import random # secret = random.randint(,) # gwea = # tries = # : # guess = int(inpu ...
- Python开发——4.集合和字符串拼接
一.集合(set) 1.集合的特性: 不同元素组成.元素是无序排列的可hash值 2.集合转为列表 s1 = {11,"hechouzi",(11,22,33)} names = ...