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. 微信H5开发

    1,line-height  不垂直居中 解决方法:用padding写高度 2,手指滑动事件    touchmove 3,页面滚动事件:touchmove 4,要写不同手机适配的基数font-siz ...

  2. js--获得当前系统时间

    window.onload = function () { var oBody = document.body; setInterval( fnTime, 1000 ); fnTime (); fun ...

  3. eclipse启动时出现无法创建java虚拟机

    最 近一直在用eclipse开发android程序,今天不知怎么的启动eclipse时就会出现Failed to create java virtual machine,无法打开eclipse程序,折 ...

  4. Maven依赖及范围

    一.依赖范围(scope): 共5种,compile (编译).test (测试).runtime (运行时).provided.system compile:编译依赖范围,在编译,测试,运行时都需要 ...

  5. 位运算------按位与、按位或、按位异或、取反、<<、>>、>>>

    程序中的所有数在计算机内存中都是以二进制的形式储存的,位运算就是直接对整数在内存中的二进制位进行操作. 知识点: 1.原码.反码.补码(以byte的1.-1举例) 示例                ...

  6. spring+shiro+ehcache整合

    1.导入jar包(pom.xml文件) <!-- ehcache缓存框架 --> <dependency> <groupId>net.sf.ehcache</ ...

  7. Shiro 基础教程

    原文地址:Shiro 基础教程 博客地址:http://www.extlight.com 一.前言 Apache Shiro 是 Java 的一个安全框架.功能强大,使用简单的Java安全框架,它为开 ...

  8. 剑指offer编程题Java实现——面试题14调整数组顺序使奇数位于偶数之前

    题目: 输入一个整数数组,实现一个函数来调整该数组中数组的顺序,使得所有的奇数位于数组的前半部分,偶数位于数组的后半部分. 解题思路:数组中维护两个指针,第一个指针初始化时候指向数组头部,第二个指针初 ...

  9. ElasticSearch权威指南学习(结构化查询)

    请求体查询 简单查询语句(lite)是一种有效的命令行adhoc查询.但是,如果你想要善用搜索,你必须使用请求体查询(request body search)API. 空查询 我们以最简单的 sear ...

  10. Linux下安装mysql5.7

    Linux下安装mysql5.7   首先准备好mysql5.7.17的安装包,安装包放在  /data/software 目录下 进入到 /usr/local 目录下,解压mysql安装包 命令: ...