170:两数之和 III - 数据结构设计
This commit is contained in:
parent
1d4e333fb8
commit
2bb6a51b80
@ -0,0 +1,103 @@
|
||||
//设计一个接收整数流的数据结构,该数据结构支持检查是否存在两数之和等于特定值。
|
||||
//
|
||||
// 实现 TwoSum 类:
|
||||
//
|
||||
//
|
||||
// TwoSum() 使用空数组初始化 TwoSum 对象
|
||||
// void add(int number) 向数据结构添加一个数 number
|
||||
// boolean find(int value) 寻找数据结构中是否存在一对整数,使得两数之和与给定的值相等。如果存在,返回 true ;否则,返回
|
||||
//false 。
|
||||
//
|
||||
//
|
||||
//
|
||||
//
|
||||
// 示例:
|
||||
//
|
||||
//
|
||||
//输入:
|
||||
//["TwoSum", "add", "add", "add", "find", "find"]
|
||||
//[[], [1], [3], [5], [4], [7]]
|
||||
//输出:
|
||||
//[null, null, null, null, true, false]
|
||||
//
|
||||
//解释:
|
||||
//TwoSum twoSum = new TwoSum();
|
||||
//twoSum.add(1); // [] --> [1]
|
||||
//twoSum.add(3); // [1] --> [1,3]
|
||||
//twoSum.add(5); // [1,3] --> [1,3,5]
|
||||
//twoSum.find(4); // 1 + 3 = 4,返回 true
|
||||
//twoSum.find(7); // 没有两个整数加起来等于 7 ,返回 false
|
||||
//
|
||||
//
|
||||
//
|
||||
// 提示:
|
||||
//
|
||||
//
|
||||
// -10⁵ <= number <= 10⁵
|
||||
// -2³¹ <= value <= 2³¹ - 1
|
||||
// 最多调用 5 * 10⁴ 次 add 和 find
|
||||
//
|
||||
// Related Topics 设计 数组 哈希表 双指针 数据流 👍 53 👎 0
|
||||
|
||||
package leetcode.editor.cn;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
//170:两数之和 III - 数据结构设计
|
||||
class TwoSumIiiDataStructureDesign {
|
||||
public static void main(String[] args) {
|
||||
//测试代码
|
||||
// Solution solution = new TwoSumIiiDataStructureDesign().new Solution();
|
||||
}
|
||||
|
||||
//力扣代码
|
||||
//leetcode submit region begin(Prohibit modification and deletion)
|
||||
class TwoSum {
|
||||
List<Integer> list;
|
||||
|
||||
/**
|
||||
* Initialize your data structure here.
|
||||
*/
|
||||
public TwoSum() {
|
||||
list = new ArrayList<>();
|
||||
}
|
||||
|
||||
/**
|
||||
* Add the number to an internal data structure..
|
||||
*/
|
||||
public void add(int number) {
|
||||
list.add(number);
|
||||
}
|
||||
|
||||
/**
|
||||
* Find if there exists any pair of numbers which sum is equal to the value.
|
||||
*/
|
||||
public boolean find(int value) {
|
||||
Collections.sort(list);
|
||||
int start = 0;
|
||||
int end = list.size() - 1;
|
||||
while (start < end) {
|
||||
if (list.get(start) + list.get(end) == value) {
|
||||
return true;
|
||||
}
|
||||
if (list.get(start) + list.get(end) > value) {
|
||||
end--;
|
||||
} else {
|
||||
start++;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Your TwoSum object will be instantiated and called as such:
|
||||
* TwoSum obj = new TwoSum();
|
||||
* obj.add(number);
|
||||
* boolean param_2 = obj.find(value);
|
||||
*/
|
||||
//leetcode submit region end(Prohibit modification and deletion)
|
||||
|
||||
}
|
@ -0,0 +1,39 @@
|
||||
<p>设计一个接收整数流的数据结构,该数据结构支持检查是否存在两数之和等于特定值。</p>
|
||||
|
||||
<p>实现 <code>TwoSum</code> 类:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>TwoSum()</code> 使用空数组初始化 <code>TwoSum</code> 对象</li>
|
||||
<li><code>void add(int number)</code> 向数据结构添加一个数 <code>number</code></li>
|
||||
<li><code>boolean find(int value)</code> 寻找数据结构中是否存在一对整数,使得两数之和与给定的值相等。如果存在,返回 <code>true</code> ;否则,返回 <code>false</code> 。</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>
|
||||
["TwoSum", "add", "add", "add", "find", "find"]
|
||||
[[], [1], [3], [5], [4], [7]]
|
||||
<strong>输出:</strong>
|
||||
[null, null, null, null, true, false]
|
||||
|
||||
<strong>解释:</strong>
|
||||
TwoSum twoSum = new TwoSum();
|
||||
twoSum.add(1); // [] --> [1]
|
||||
twoSum.add(3); // [1] --> [1,3]
|
||||
twoSum.add(5); // [1,3] --> [1,3,5]
|
||||
twoSum.find(4); // 1 + 3 = 4,返回 true
|
||||
twoSum.find(7); // 没有两个整数加起来等于 7 ,返回 false</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>-10<sup>5</sup> <= number <= 10<sup>5</sup></code></li>
|
||||
<li><code>-2<sup>31</sup> <= value <= 2<sup>31</sup> - 1</code></li>
|
||||
<li>最多调用 <code>5 * 10<sup>4</sup></code> 次 <code>add</code> 和 <code>find</code></li>
|
||||
</ul>
|
||||
<div><div>Related Topics</div><div><li>设计</li><li>数组</li><li>哈希表</li><li>双指针</li><li>数据流</li></div></div><br><div><li>👍 53</li><li>👎 0</li></div>
|
Loading…
Reference in New Issue
Block a user