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 - ...
随机推荐
- django.core.exceptions.ImproperlyConfigured: Requested setting DEFAULT_INDEX_TABLESPACE, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call
Error info: django.core.exceptions.ImproperlyConfigured: Requested setting DEFAULT_INDEX_TABLESPACE, ...
- Python开发——函数【装饰器、高阶函数、函数嵌套、闭包】
装饰器 装饰器本质就是函数,为其他函数添加附加功能. 原则: 不修改被修饰函数的源代码 不修改被修饰函数的调用方法 装饰器知识储备:装饰器 = 高阶函数 + 函数嵌套 + 闭包 案例:求函数运行时间! ...
- android app主程序启动前加载图片
android app加载启动图片需要新创建一个activity,在主activity先加载图片activity,展示过程结束后,显示主activity.具体流程如下: 一.创建图片activity的 ...
- SPOJ - AMR11B
题目链接:https://www.spoj.com/problems/AMR11B/en/ 题目大意就是要你求图形覆盖的格点数,标记每个图形里的未标记格点(包括边界),总标记数就是覆盖的总格点数. # ...
- AX_HelpGenerator
HelpGenerator helpGenerator; ; helpGenerator = infolog.helpGenerator(); helpGenerator.showURL(" ...
- Android 软件管理工具类Utils
Android 软件管理工具类Utils /** * Created by uilubo on 2015/9/30. * 工具类 */ public class Utils { public stat ...
- html-day06
html-day06 1.定位 定位: 1.普通流定位 普通流,又称为文档流 块级元素:从上到下一个一个的排列 行内元素:一行内从左到右的排列 2.浮动定位 1.什么是浮动定位 将元素排除在普通流之外 ...
- linux下设置计划任务执行python脚本
linux下设置计划任务执行python脚本 简介 crontab命令被用来提交和管理用户的需要周期性执行的任务,与windows下的计划任务类似,当安装完成操作系统后,默认会安装此服务工具,并且会自 ...
- Forward团队-爬虫豆瓣top250项目-模块测试
项目托管平台地址:https://github.com/xyhcq/top250 模块测试:爬虫对信息的处理部分 测试方法: 实际运行一下代码: 可以看见,信息都已经爬取出来了 其他补充说明: 原本系 ...
- EF6学习笔记(四) 弹性连接及命令拦截调试
EF6学习笔记总目录:ASP.NET MVC5 及 EF6 学习笔记 - (目录整理) 本章原文地址:Connection Resiliency and Command Interception 原文 ...