/*
* 170. Two Sum III - Data structure design My Submissions QuestionEditorial Solution
Total Accepted: 13104 Total Submissions: 55431 Difficulty: Easy
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
Hide Company Tags LinkedIn
Hide Tags Hash Table Design
Show Similar Problems
*/
package ood;
import java.util.*;
public class TwoSumDesign{
HashMap<Integer, Integer> map = new HashMap<>(); //記錄數(shù)字和它出現(xiàn)的次數(shù)
List<Integer> nums = new ArrayList<>(); // 記錄添加的元素
// https://discuss.leetcode.com/topic/32786/beats-100-java-code
// Add the number to an internal data structure.
public void add(int number) {
if (map.containsKey(number)) {
map.put(number, map.get(number) + 1);
} else {
map.put(number, 1);
nums.add(number);
}
}
// Find if there exists any pair of numbers which sum is equal to the value.
public boolean find(int value) {
for (int i = 0; i < nums.size(); i++) {
int num1 = nums.get(i), num2 = value - num1;
if ((num1 == num2 && map.get(num1) > 1) || (num1 != num2 && map.containsKey(num2))) {
return true;
}
}
return false;
}
public static void main(String[] args) {
// Your TwoSum object will be instantiated and called as such:
TwoSumDesign twoSum = new TwoSumDesign();
twoSum.testcase1();
twoSum.testcase2();
twoSum.testcase3();
}
public void testcase1() {
TwoSumDesign twoSum = new TwoSumDesign();
for (int number : new int[] { 1, 3, 5, 8 }) {
twoSum.add(number);
}
int value = 4;
System.out.println(" find(" + value + ") = " + twoSum.find(value));
value = 7;
System.out.println(" find(" + value + ") = " + twoSum.find(value));
}
public void testcase2() {
TwoSumDesign twoSum = new TwoSumDesign();
int number = 0;
twoSum.add(number);
int value = 0;
System.out.println(" find(" + value + ") = " + twoSum.find(value));
}
// [add(3),add(2),add(1),find(2),find(3),find(4),find(5),find(6)]
public void testcase3() {
TwoSumDesign twoSum = new TwoSumDesign();
twoSum.add(3);
twoSum.add(2);
twoSum.add(1);
int value = 2;
System.out.println(" find(" + value + ") = " + twoSum.find(value)); // false
value = 3;
System.out.println(" find(" + value + ") = " + twoSum.find(value)); // true
value = 4;
System.out.println(" find(" + value + ") = " + twoSum.find(value)); // true
value = 5;
System.out.println(" find(" + value + ") = " + twoSum.find(value)); // true
value = 6;
System.out.println(" find(" + value + ") = " + twoSum.find(value)); // false
}
}
170. Two Sum III - Data structure design
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。
相關(guān)閱讀更多精彩內(nèi)容
- 這個(gè)數(shù)據(jù)結(jié)構(gòu)主要是把數(shù)據(jù)存儲(chǔ)到unordered_multiset里,multiset類似set,但是它允許重復(fù)元...
- Design and implement a TwoSum class. It should support th...
- Design and implement a TwoSum class. It should support th...