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 - ...
随机推荐
- KD-树(上)
来自于https://zhuanlan.zhihu.com/p/23966698 思路篇 导语:kd 树是一种二叉树数据结构,可以用来进行高效的 kNN 计算.kd 树算法偏于复杂,本篇将先介绍以二叉 ...
- 《C#从现象到本质》读书笔记(九)第11章C#的数据结构
<C#从现象到本质>读书笔记(九)第11章C#的数据结构 C#中的数据结构可以分为两类:非泛型数据结构和泛型数据结构. 通常迭代器接口需要实现的方法有:1)hasNext,是否还有下一个元 ...
- PHP脚本命令行执行成功,CRON无法执行故障解决记录
先来看看一个最简单的PHP文件(ip.php) <?php $myip = get_ip_cmd(); echo($myip); // get ip address function get_i ...
- 2018上IEC计算机高级语言(C)作业 第3次作业
2018上IEC计算机高级语言(C)作业 第3次作业 一.例程调试(20分) 调试下面2个例程,各位同学调试用自己的学号模3(即除以3取余数)加1序号及该序号乘以2的题.写明调试过程,如错误现象(如给 ...
- Python Day 5
阅读目录: 数字类型: 字符串类型: 列表类型: 可变与不可变类型: ##数字类型: # 了了解:py2中小整数用int存放,大整数用long # 1.整型 num = -10000000000000 ...
- kbmmw 5.07 正式发布
来了来了 5.07.00 Dec 9 2018 Important notes (changes that may break existing code) === ...
- C++ MFC棋牌类小游戏day4
根据昨天的计划,今天开始做下面的内容. 1.鼠标点击事件 2.点击坐标进行处理.(坐标转换) 3.判断选中的位置是否有效. 4.确定选中的棋子,设置棋子的状态和棋子所在坐标的状态. 5.判断移动是否有 ...
- C++ MFC棋牌类小游戏day3
今天开始设计小人棋子. 画法跟画虎一样,唯一不一样的是小人在刚开始会有重叠的情况,所以画起来可能比虎的棋子能够难一点. 我打算用Location结构体中的num来标记每个棋盘坐标存在棋子的个数,isH ...
- 向Word添加一段文本
文档层次结构 [段落之后] 是一段连续文本,它定义具有一组常见属性的文本区域.一段连续文本由 r 元素表示,这样创建器便可组合换行.样式或格式设置属性,从而将相同信息应用于一段连续文本的所有部分. 正 ...
- 删除sheet
/// <summary> /// 删除sheet /// </summary> /// <param name="fileName">< ...