[LeetCode]1. 2Sum 数组中和为特定值的两个数

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

 

解法一:暴力搜索法,时间复杂度O(n^2)

class Solution {
public:
    vector<int> twoSum(vector<int> &numbers, int target) {
        vector<int> res;
        for (int i = 0; i < numbers.size(); ++i) {
            for (int j = i + 1; j < numbers.size(); ++j) {
                if (numbers[j] + numbers[i] == target) {
                    res.push_back(i + 1);
                    res.push_back(j + 1);
                }
            }
        }
        return res;
    }
};

 

解法二:考虑先将数组arr排序,利用两个指针left和right指向排好序数组的某两个值,初始化left=0,right=len-1,其中len为数组长度。当arr[left]+arr[right]=sum时,则找到答案返回;当arr[left]+arr[right]>sum,时,right--;当arr[left]+arr[right]<sum时,left++。循环上述过程,若找到则返回,否则直至left=right说明没有解。最后在原数组中找到符合要求的两个数的位置即可。时间复杂度O(nlogn)。

class Solution {
public:
    vector<int> twoSum(vector<int>& nums, int target) {
        vector<int> vi(nums.begin(), nums.end());
        sort(vi.begin(), vi.end());
        
        int index1 = 0;
        int index2 = nums.size() - 1;
        while(index1 < index2)
        {
            if(vi[index1] + vi[index2] == target)
                break;
            else if(vi[index1] + vi[index2] < target)
                index1++;
            else
                index2--;
        }
        
        vector<int> res(2, 0);
        int i = 0;
        for (; i < nums.size(); i++)
        {
            if (nums[i] == vi[index1])
            {
                res[0] = i + 1;
                break;
            }
        }
        if (vi[index1] == vi[index2])
            i++;
        else
            i = 0;
        for (; i < nums.size(); i++)
        {
            if (nums[i] == vi[index2])
            {
                res[1] = i + 1;
                break;
            }
        }
        sort(res.begin(), res.end());
        
        return res;
    }
};

 

解法三:考虑使用STL的Map进行查找。先遍历一次数组,键为元素值,值为元素在数组中的下标。然后再从头开始遍历数组,在Map中找target=sum-arr[i]的元素,若存在则返回,否则直至i=len-1说明没有解。最后找到target在arr中的位置即可。时间复杂度O(n)。

class Solution {
public:
    vector<int> twoSum(vector<int> &numbers, int target) {
        vector<int> res;
        map<int, int> numMap;
        for (int i = 0; i < numbers.size(); ++i) {
            numMap[numbers[i]] = i;
        }
        for (int i = 0; i < numbers.size(); ++i) {
            int tmp = target - numbers[i];
            if (numMap.find(tmp) != numMap.end() && numMap[tmp] != i) {
                res.push_back(i + 1);
                res.push_back(numMap[tmp] + 1);
                break;
            }
        }
        return res;
    }
};

 

 

posted @ 2015-09-20 15:41  AprilCheny  阅读(1919)  评论(0编辑  收藏  举报