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的更多相关文章

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

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

  2. LeetCode Two Sum III - Data structure design

    原题链接在这里:https://leetcode.com/problems/two-sum-iii-data-structure-design/ 题目: Design and implement a ...

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

  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

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

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

    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 设计two sum模式 --------- java

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

  8. LeetCode 笔记27 Two Sum III - Data structure design

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

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

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

  10. [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 - ...

随机推荐

  1. java【基础】日期操作

    主要是date类,SimpleDateFormat类以及Calendar类的使用. date表示日期,simpleDateFormat 表示日期格式化,Calendar一般用来做时间的操作,比如加减天 ...

  2. pandas 导入导出

    pandas可以读取与存取的资料格式有很多种,像csv.excel.json.html与pickle等… 1.读取csv import pandas as pd #加载模块 #读取csv data = ...

  3. windows下Anaconda的安装与配置正解

    一.下载anaconda 第一步当然是下载anaconda了,官方网站的下载需要用迅雷才能快点,或者直接到清华大学镜像站下载. 清华大学提供了镜像,从这个镜像下载速度很快,地址: https://mi ...

  4. cpp 区块链模拟示例(五) 序列化

    有了区块和区块链的基本结构,有了工作量证明,我们已经可以开始挖矿了.剩下就是最核心的功能-交易,但是在开始实现交易这一重大功能之前,我们还要预先做一些铺垫,比如数据的序列化和启动命令解析. 根据< ...

  5. copy other

    DELPHI基础开发技巧 ◇[DELPHI]网络邻居复制文件 uses shellapi; copyfile(pchar('newfile.txt'),pchar('//computername/di ...

  6. AX_SysExcel

    void KTL_CPeng_ImportCustStamp()  {      str                 file;      FileNameFilter      filter = ...

  7. Maven学习 二 Maven环境搭建

    第一步:下载Maven并解压 注意选择镜像地址,选择国内的会快点 解压目录 Maven目录分析 bin:含有mvn运行的脚本 boot:含有plexus-classworlds类加载器框架 conf: ...

  8. easyui中的option设置selected没有效果

    在mvc中,使用了easyUI框架,然后我点击添加新消息的时候弹窗一个新窗口,添加信息.却发现我的select一直无法设置默认状态,jq选中,还是直接在添加一条请选择的option都没办法选中,网络上 ...

  9. 2019.03.01 bzoj3075: [Usaco2013]Necklace(kmp+dp)

    传送门 题意简述:给出S,TS,TS,T两个字串,∣S∣≤10000,∣T∣≤1000|S|\le10000,|T|\le1000∣S∣≤10000,∣T∣≤1000,问至少从SSS中删去几个字符能够 ...

  10. (PMP)第11章-----项目风险管理

    11.1 规划风险管理 输入 工具与技术 输出 1.项目章程 2.项目管理计划 (所有组件) 3.项目文件 (相关方登记册) 4.事业环境因素 5.组织过程资产 1.专家判断 2.数据分析 (相关方分 ...