Two Sum - Data structure design
2017-10-19 14:51
288 查看
Design and implement a TwoSum class. It should support the following operations:
sum is equal to the value.
java
public class TwoSum {
/*
* Add the number to an internal data structure.
* @param number: An integer
* @return: nothing
*/
Map<Integer, Integer> map = null;
List<Integer> list = null;
public TwoSum() {
map = new HashMap<>();
list = new ArrayList<>();
}
public void add(int number) {
// write your code here
if (map.containsKey(number)) {
map.put(number, map.get(number) + 1);
} else {
map.put(number, 1);
list.add(number);
}
}
/*
* @param value: An integer
* @return: Find if there exists any pair of numbers which sum is equal to the value.
*/
public boolean find(int value) {
// write your code here
for (int i = 0; i < list.size(); i++) {
int num = list.get(i);
int target = value - num;
if (num == target && map.get(num) > 1) {
return true;
} else if (num != target && map.containsKey(target)) {
return true;
} else {
continue;
}
}
return false;
}
}
// Your TwoSum object will be instantiated and called as such:
// TwoSum twoSum = new TwoSum();
// twoSum.add(number);
// twoSum.find(value);
python
class TwoSum:
"""
Add the number to an internal data structure.
@param: number: An integer
@return: nothing
"""
def __init__(self):
self.mapping = {}
def add(self, number):
# write your code here
if number in self.mapping:
self.mapping[number] += 1
else:
self.mapping[number] = 1
"""
@param: value: An integer
@return: Find if there exists any pair of numbers which sum is equal to the value.
"""
def find(self, value):
# write your code here
for ele in self.mapping:
target = value - ele
if target == ele and self.mapping[ele] > 1:
return True
elif target != ele and target in self.mapping:
return True
else:
continue
return False
# Your TwoSum object will be instantiated and called as such:
# twoSum = TwoSum()
# twoSum.add(number)
# twoSum.find(value)
addand
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.
java
public class TwoSum {
/*
* Add the number to an internal data structure.
* @param number: An integer
* @return: nothing
*/
Map<Integer, Integer> map = null;
List<Integer> list = null;
public TwoSum() {
map = new HashMap<>();
list = new ArrayList<>();
}
public void add(int number) {
// write your code here
if (map.containsKey(number)) {
map.put(number, map.get(number) + 1);
} else {
map.put(number, 1);
list.add(number);
}
}
/*
* @param value: An integer
* @return: Find if there exists any pair of numbers which sum is equal to the value.
*/
public boolean find(int value) {
// write your code here
for (int i = 0; i < list.size(); i++) {
int num = list.get(i);
int target = value - num;
if (num == target && map.get(num) > 1) {
return true;
} else if (num != target && map.containsKey(target)) {
return true;
} else {
continue;
}
}
return false;
}
}
// Your TwoSum object will be instantiated and called as such:
// TwoSum twoSum = new TwoSum();
// twoSum.add(number);
// twoSum.find(value);
python
class TwoSum:
"""
Add the number to an internal data structure.
@param: number: An integer
@return: nothing
"""
def __init__(self):
self.mapping = {}
def add(self, number):
# write your code here
if number in self.mapping:
self.mapping[number] += 1
else:
self.mapping[number] = 1
"""
@param: value: An integer
@return: Find if there exists any pair of numbers which sum is equal to the value.
"""
def find(self, value):
# write your code here
for ele in self.mapping:
target = value - ele
if target == ele and self.mapping[ele] > 1:
return True
elif target != ele and target in self.mapping:
return True
else:
continue
return False
# Your TwoSum object will be instantiated and called as such:
# twoSum = TwoSum()
# twoSum.add(number)
# twoSum.find(value)
相关文章推荐
- LeetCode - Two Sum III - Data structure design
- leetcode3 Two Sum III – Data structure design
- [LeetCode] Two Sum III - Data structure design 两数之和之三 - 数据结构设计
- Two Sum III - Data structure design
- Two Sum III - Data structure design
- leetcode[170]Two Sum III - Data structure design
- Two Sum III - Data structure design
- Leetcode|Two Sum III - Data structure design
- Two Sum III - Data Structure Design
- LeetCode-Two Sum III - Data structure design
- LeetCode Two Sum III - Data structure design
- Two Sum III - Data structure design
- [leecode 170] Two Sum III - Data structure design
- 170 Two Sum III - Data structure design
- [leetcode]Two Sum III - Data structure design
- [Leetcode] Two Sum III - Data structure design
- 170. Two Sum III - Data structure design
- [LeetCode] Two Sum III - Data structure design 两数之和之三
- Leetcode: Two Sum III - Data structure design
- Easy 170题 Two Sum III - Data structure design