leetcode170. 两数之和 III - 数据结构设计

设计并实现一个 TwoSum 的类,使该类需要支持 add 和 find 的操作。

add 操作 -  对内部数据结构增加一个数。
find 操作 - 寻找内部数据结构中是否存在一对整数,使得两数之和与给定的数相等。

示例 1:

add(1); add(3); add(5);
find(4) -> true
find(7) -> false
示例 2:

add(3); add(1); add(2);
find(3) -> true
find(6) -> false

在列表有序的情况下,可以使用双指针,时间为O(N),插入一个数字的时间为O(N)。

可以使用哈希表,查找的时间为O(N),插入时间为O(1)。

两种方法空间都是O(N)。

综上所述,没有排序的列表没有必要为了解题去排序。

设计此数据结构时,也是使用哈希表最好。

import java.util.HashMap;

class TwoSum {
  private HashMap<Integer, Integer> countsMap;

  /** Initialize your data structure here. */
  public TwoSum() {
    countsMap = new HashMap<Integer, Integer>();
  }

  /** Add the number to an internal data structure.. */
  public void add(int number) {
    if (countsMap.containsKey(number))
      countsMap.replace(number, countsMap.get(number) + 1);
    else
      countsMap.put(number, 1);
  }

  /** Find if there exists any pair of numbers which sum is equal to the value. */
  public boolean find(int value) {
    for (Map.Entry<Integer, Integer> entry : countsMap.entrySet()) {
      int key = value - entry.getKey();
      if ((key == entry.getKey() && entry.getValue() > 1)
       || (key != entry.getKey() && countsMap.containsKey(key))) {
        return true;
      }
    }
    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);
 */
发布了537 篇原创文章 · 获赞 1万+ · 访问量 131万+

猜你喜欢

转载自blog.csdn.net/hebtu666/article/details/104304423