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
Map<Integer, Integer> dict = new HashMap<Integer, Integer>(); public void add(int number) { if (dict.containsKey(number)) { dict.put(number, dict.get(number) + 1); } else { dict.put(number, 1); } } public boolean find(int value) { for (Integer key : dict.keySet()) { if (value - key == key) { if (dict.get(key) >= 2) { return true; } } else if (dict.containsKey(value - key)) { return true; } } return false; }
相关推荐
主要介绍了LeetCode -- Path Sum III分析及实现方法的相关资料,希望通过本文能帮助到大家,需要的朋友可以参考下
大佬的leetcode刷题笔记(c++版本)
leetcode中国算法和数据结构 算法与数据结构之道 我在这个项目中编写了一些数据结构和算法的 C++ 实现。 你可以阅读关于我的中文体验。 顺便说一下,很多链接可能是中文的。 完毕 选择排序 字符串类 插入排序 我尽力...
leetcode-cli-plugins leetcode-cli 的第 3 方插件。 什么是 如何使用 如何使用 插件 名称 描述 增强的命令 按公司或标签过滤问题 list 不要在同一台计算机上使 Chrome 的会话过期 login 不要在同一台计算机上使 ...
leetcode 接口 该项目可帮助您使用首选的 IDE 或带有命令行界面 (CLI) 的编辑器来执行 ...two-sum leetcode --name add-two-numbers leetcode --name median-of-two-sorted-arrays 然后问题描述和启动代码将自动
Two-Sum leetcode两数之和代码 题目描述:给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。 你可以假设每种输入只会对应一个答案。但是,数组...
leetcode-训练 算法训练。 java $: javac hello.java java $: java hello c $: gcc hello.c 如果没有错误会生成a.out 可执行文件 c $: ./a.out leetcode cli leetcode version leetcode help leetcode help user ...
LeetCode题解 - Java语言实现-181页.pdf
leetcode 答案Leetcode---数据库 我对 Leetcode 数据库问题的回答
leetcode-1-Two-Sum 这是我在 Github 的第一天。 我只是 AC leetcode 中的第一个问题。 从现在开始,我将使用Github在leetcode中记录我的生活。 我擅长 C++,但对 java 和 Python 不是很专业,我将使用最流行的 3 种...
彩色版本 正版 pdf 精讲数据结构 + 算法 链表 树 图表 贪心算法 指针 动态规划 查找算法
leetcode1-300.docx
leetcode-cli 一个享受 leetcode 的高效 cli 工具! 非常感谢 leetcode.com,一个非常棒的网站! ⦙⦙⦙⦙⦙⦙⦙⦙ 一个很打问题的方法。 问题来缓解离线思考。 编码前的源代码。 居住和与 leetcode.com。 下载你...
leetcode 答案leetcode--python Leetcode 的答案
Algorithm-LeetCode-Sol-Res.zip,干净,易懂的解决方案和资源,为leetcode在线判断算法问题。,算法是为计算机程序高效、彻底地完成任务而创建的一组详细的准则。
答案LeetCode_1_TwoSum LeetCode 问题:给定一个整数数组,找出两个数字,使它们相加为特定的目标数字。 函数 twoSum 应该返回两个数字的索引,使它们相加为目标,其中 index1 必须小于 index2。 请注意,您返回的...