Two Sum

Given an array of integers, find two numbers such that they add up to a specific target number.

The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note that your returned answers (both index1 and index2) are not zero-based.

You may assume that each input would have exactly one solution.

Input: numbers={2, 7, 11, 15}, target=9

Output: index1=1, index2=2

  題意是從一堆無序的數中,找到兩個數,使其和等於給定值。分析可以知道,有幾種方法,既然是無序的,可以考慮先排序,然後用雙指針從頭到尾移動,知道找到合適的兩個數,這要注意對下標的保存,因爲題目要求返回的是下標;

  第二種方法,不用排序,對於給定的值i,找數組中是否存在另外一個值target - i,存在則返回這兩個值的下標,這要藉助於hashTable來O(1)的查找。

 第三種方法就是,直接暴力,枚舉所有的兩個數的和的可能性,這樣時間複雜度很高,達到了O(N^2)

直接查找:

class Solution {
public:
    //直接查找
    vector<int> twoSum(vector<int>& nums, int target) {
        unsigned numLen = nums.size();
        vector<int> ans;
        if(numLen > 1)
        {
            unordered_map<int, set<int>> hmap;
            for(unsigned i = 0; i < numLen; ++i)
            {
                hmap[nums[i]].insert(i + 1);
            }
            
            int tmpTarget;
            for(unsigned i = 0; i < numLen; ++i)
            {
                tmpTarget = target - nums[i];
                if(tmpTarget == nums[i] && hmap[tmpTarget].size() >= 2)
                {
                    ans.push_back(*(hmap[nums[i]].begin()));
                    hmap[nums[i]].erase(hmap[nums[i]].begin());
                    ans.push_back(*(hmap[nums[i]].begin()));
                    return ans;
                }
                else if(tmpTarget != nums[i] && hmap.find(tmpTarget) != hmap.end())
                {
                    ans.push_back(i + 1);
                    ans.push_back(*(hmap[tmpTarget].begin()));
                    return ans;
                }
            }
        }
        
        return ans;
    }
};

排序+雙指針

typedef struct Node
{
    int val;
    int index;
    Node(int v, int idx) : val(v), index(idx) {}
}Node;

bool comp(const Node &a, const Node &b)
{
    if(a.val == b.val)
        return a.index < b.index;
    return a.val < b.val;
}

class Solution {
public:
    vector<int> twoSum(vector<int>& nums, int target) {
        int numLen = nums.size();
        vector<int> ans;
        if(numLen > 1)
        {
            vector<Node> NodeVec;
            for(unsigned i = 0; i < numLen; ++i)
            {
                NodeVec.push_back(Node(nums[i], i + 1));
            }
            
            sort(NodeVec.begin(), NodeVec.end(), comp);
            int low = 0, high = numLen - 1, tmpSum = 0;
            while(low < high)
            {
                tmpSum = NodeVec[low].val + NodeVec[high].val;
                if(tmpSum == target)
                {
                    ans.push_back((NodeVec[low].index < NodeVec[high].index ? NodeVec[low].index : NodeVec[high].index));
                    ans.push_back((NodeVec[low].index > NodeVec[high].index ? NodeVec[low].index : NodeVec[high].index));
                    return ans;
                }
                else if(tmpSum < target)
                {
                    ++low;
                }
                else
                    --high;
            }
        }
        
        return ans;
    }
};


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章