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

设计一个two sum 的类。包含add和find两种操作方式。

1、使用HashMap和HashSet实现。

public class TwoSum {

    Map<Integer,Integer> map = new HashMap<Integer,Integer>();
Set<Integer> set = new HashSet<Integer>();
// Add the number to an internal data structure.
public void add(int number) {
if (set.contains(number)){
map.put(number, 2);
}else {
set.add(number);
map.put(number, 1);
}
} // Find if there exists any pair of numbers which sum is equal to the value.
public boolean find(int value) {
for (int num : set){
if (num == value - num){
if (map.get(num) == 2){
return true;
}
} else if (set.contains(value - num)){
return true;
}
}
return false;
}
} // Your TwoSum object will be instantiated and called as such:
// TwoSum twoSum = new TwoSum();
// twoSum.add(number);
// twoSum.find(value);

2、使用HashMap 和 List(也可以只使用一个HashMap,记录一或者2即可。)

public class TwoSum {

    private List<Integer> list = new ArrayList<Integer>();
private Map<Integer, Integer> map = new HashMap<Integer, Integer>(); // 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);
list.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 < list.size(); i++){
int num1 = list.get(i), num2 = value - num1;
if ((num1 == num2 && map.get(num1) > 1) || (num1 != num2 && map.containsKey(num2))) return true;
}
return false;
}
} // Your TwoSum object will be instantiated and called as such:
// TwoSum twoSum = new TwoSum();
// twoSum.add(number);
// twoSum.find(value);

3、为什么要使用一个List,因为在遍历操作的时候,List要比Map或者Set快得多,使用一个List可以用空间换取时间。

最新文章

  1. 关于包含pom.xml的开源项目如何导入
  2. [debian]SublimeText>PrettyCode無效
  3. 图片延迟加载(lazyload)的实现原理
  4. Inode详解-重要
  5. SOAP和WSDL的一些必要知识(转)
  6. mysql show
  7. CSS3 Filter
  8. HW7.16
  9. 谈memcache和memcached的区别
  10. IPMITOOL常用操作指令
  11. OpenJDK1.8.0 源码解析————HashMap的实现(二)
  12. java 制作QQ登录界面
  13. org.springframework.core.NestedIOException: ASM ClassReader failed to parse class file - probably du
  14. Analysis of Servlet
  15. Codeforces 671D Roads in Yusland [树形DP,线段树合并]
  16. Stiring公式证明
  17. for循环.html
  18. POJ 1146
  19. 比较两个JSON字符串是否完全相等
  20. lamp-linux2

热门文章

  1. my understanding of (lower bound,upper bound) binary search, in C++, thanks to two post 分类: leetcode 2015-08-01 14:35 113人阅读 评论(0) 收藏
  2. 两个APP跳转传值问题
  3. ICEM相关
  4. Nutch2.x
  5. PHP store session with couchbase
  6. win7下Outlook2010禁止访问具有潜在不安全因素的附件的解决办法
  7. ANDROID : java.lang.NoSuchMethodError: org.apache.commons.codec.binary.Base64.encodeBase64String in android
  8. DateTimePicker如何与Delphi自带Style同步
  9. JS在火狐浏览器下如何关闭标签?
  10. Java和eclipse常用操作