LeetCode #1 TwoSum 哈希表 双指针

Description


 

Given an array of integers, return indices of the two numbers such that they add up to a specific target.

You may assume that each input would have exactly one solution, and you may not use the same element twice.

Example:

Given nums = [2, 7, 11, 15], target = 9,

Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].

 

思路


 

  首先我们想到的是暴力法,两个 for 循环求解,时间复杂度为O(n^2)

  

//Runtime: 106 ms
//First thought: BF
class Solution {
    public:
    vector<int> twoSum(vector<int>& nums, int target) {             
        int i,j;
        vector<int> ret {0, 1};
        for (i = 0; i< nums.size(); ++i) {
            ret[0] = i;
            for(j = i+1; j < nums.size(); ++j){
                if (nums[i] + nums[j] == target) 
                {    
                    ret[1] = j;
                    return ret;
                }                                                          
            }                                                              
        }                                 
    }                                                                    
};         

 

  但是,我发现时间消耗太多了,所以借鉴当时“换硬币”、“爬楼梯”问题的优化方法,由于num[i]、num[j]、target都是定值,所以在条件判断里不需要每次都进行加运算

//Runtime: 79 ms
//Because nums[i], nums[j], target are fixed values, we do not need to do additions every time
class Solution {
    public:
    vector<int> twoSum(vector<int>& nums, int target) {             
        int i,j;
        vector<int> ret {0, 1};
        for (i = 0; i< nums.size(); ++i) {
            ret[0] = i;
            int tar = target - nums[i];
            for(j = i+1; j < nums.size(); ++j){
                if (nums[j] == tar) 
                {    
                    ret[1] = j;
                    return ret;
                }                                                          
            }                                                              
        }                                 
    }                                                                    
}; 

 

  AC后,采用更复杂的数据结构 散列表(HashTable)以降低时间复杂度,我的想法是这样: 如果想只扫描一遍数组就得出结果,那么肯定就要有一部字典,边扫描边存储值,在这里存储的不是数组当前的值,而是“目标值 - 当前值”。也就是说,字典里存储 Key 的是每个数据所希望的”另一半“的大小。而 字典的 Value 是数组索引。然后我们再往后扫描,如果扫描到的值的另一半出现在了字典里,那么说明当前值是”上一半“所需要的”下一半“,返回坐标;如果没有字典里没有出现它的另一半,那么把差值和当前索引继续存储字典中。

  该算法的时间复杂度为 O(n)

  实现比较简洁,如果注重效率的话,可以在每轮循环把 nums[i] 暂存到一个变量,同时字典对象使用 find() 返回的迭代器来判断查找结果和修改对应的 Value。

class Solution {
public:
    vector<int> twoSum(vector<int>& nums, int target) {
        std::map<int, int> m;
        for (int i = 0; i < nums.size(); ++i) {
            if (m.count(nums[i])) {
                return {i, m[nums[i]]};
            } else {
                m[target - nums[i]] = i;
            }
        }
        
        return {};
    }
};

 

  补充一种双指针遍历有序数组的方法(伪代码)

TwoNumberSum (S, x)
    mergeSort (S, 1, n)
    i = 1
    j = n
    while i < j
        if A[i] + A[j] == x
            return true            
        if A[i] + A[j] < x
            i = i + 1
        if A[i] + A[j] > x
            j = j - 1
    return false  

  

  双指针一般要排序,排序会打乱下标,而本题让我们返回的是原始数组的下标而不是两个元素值,所以不方便采用

  下面这么写就是因为这个坑点而 WA 了

 

class Solution {
public:
    vector<int> twoSum(vector<int>& nums, int target) {
        std::sort(nums.begin(), nums.end());
        int i = 0, j = nums.size() - 1;
        
        while (i < j) {
            int x = nums[i];
            int y = nums[j];
            if (x + y == target) {
                std::cout << x << std::endl;
                std::cout << y << std::endl;
                return {i, j};  // 注意,返回的是原始下标,所以还需要找到排序之前的映射,不方便
            } else if (x + y < target) {
                std::cout << "++i" << std::endl;
                ++i;
            } else {
                std::cout << "--j" << std::endl;
                --j;
            }
        }
        
        return {};
    }
};

 

  双指针的查找的时间仅需 Θ(n),所以时间总代价受排序时间代价的影响,为Θ(n lgn)

  扫描的原理很简单,先设 mi,j  : A[i] + A[j] < S,Mi,j : A[i] + A[j] > S 。由于序列已排序,则 mi,j ⇒∀k < j  都有 mi,k  成立,并且 Mi,j ⇒ ∀k > i 都有 Mk,j 成立

  

   

posted @ 2017-12-18 18:04  bw98  阅读(445)  评论(0编辑  收藏  举报