[LeetCode两数之和 III-数据结构设计] | 刷题打卡

298 阅读2分钟

[LeetCode两数之和 III-数据结构设计] | 刷题打卡

一直有刷题习惯,最近才看到掘金举办了刷题活动,特来参加!此题为第5题。

本文正在参与掘金团队号上线活动,点击 查看大厂春招职位

一、题目描述:

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

描述

设计b并实现一个 TwoSum 类。他需要支持以下操作:add 和 find。 add -把这个数添加到内部的数据结构。 find -是否存在任意一对数字之和等于这个值

样例

样例 1:

add(1);add(3);add(5);
find(4)//返回true
find(7)//返回false

二、思路分析:

方法描述时间复杂度空间复杂度
双指针下文代码实现add和find都是O(N)O(N)O(N)O(N)
哈希法下文代码实现add是O(1)O(1),find是$$O(N)(N)(N)

三、AC 代码:

哈希法

?> 优点:add速度快O(1),无需像双指针那样排序

import java.util.HashMap;
import java.util.Map;

public class TwoSum {
    private Map<Integer, Integer> counter;

    public TwoSum() {
        counter = new HashMap<>();
    }

    /**
     * @param number: An integer
     * @return: nothing
     */
    public void add(int number) {
        // write your code here
        // key is value ,value is [origin value + 1,0 + 1]
        counter.put(number, counter.getOrDefault(number, 0) + 1);
    }

    /**
     * @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 (Integer key : counter.keySet()) {
            // searchKey + key == value
            int searchKey = value - key;
            int reulstCount = key == searchKey ? 2 : 1;
            if (counter.getOrDefault(searchKey, 0) >= reulstCount) {
                return true;
            }
        }
        return false;
    }
}

双指针

!>缺点:数据量大时会超时

代码实现

public class TwoSum {
    List<Integer> mSums ;
    public TwoSum(){
        mSums = new ArrayList<>();
    }
    /**
     * @param number: An integer
     * @return: nothing
     */
    public void add(int number) {
        // write your code here
        mSums.add(number);
        // sort array
        int index = mSums.size() -1;
        // keep ascending order
        while(index > 0 && mSums.get(index-1) > mSums.get(index) ){
            // left value > right value
            // swap
            int temp = mSums.get(index);
            mSums.set(index,mSums.get(index-1));
            mSums.set(index-1,temp);
            index --;
        }
    }

    /**
     * @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
        int left = 0 ;
        int right = mSums.size()-1;
        while(left<right){
            int currentSum = mSums.get(left)+mSums.get(right);
            if(currentSum < value){
                left++;
            }else if(currentSum >value){
                right--;
            } else{
                return true;
            }
        }
        return false;

    }
}

四、总结:

此题考察的并不是双指针,注意审题,题目后缀是数据结构设计,即表面这道题目考察的是数据结构使用能力。

  • 使用双指针纵然可以解决这道题目,使用的是List结构,缺点是数据量大会超时,不符合题意。
  • 较为良好的实践是使用哈希表,优点是动态插入删除,无需排序,数据量大也不会超时。
  • 在做完题目后,能回答出ListMap的区别更好,一般回答思路是:
    1. 介绍两者的ADT抽象接口,即对外暴露的接口
    2. 接口底层如何实现,如果是你,你如何实现
    3. 访问的区别,谁动态访问,谁静态访问
    4. 操作的区别,谁动态操作,谁静态操作