[Leetcode] Two Sum

[Leetcode] Two Sum

1. Question in English

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

2. Question in Chinese

从一个给定的整型数组找出两个数,使得这两个整数的和等于特定的值。
函数twoSum应该返回这两个数的下标,其中index1小于index2,此外需要注意的是index1和index2不是基于零,而是基于1的。
你可以假设每一个输入有且只有一个正确结果。

输入: numbers={2, 7, 11, 15}, target=9
输出:index1=1, index2=2

3.分析

3.1 暴力解法

一开始看到这题的时候,首先想到的就是暴力解法,两个for循环,时间复杂度 O(N^2)。本以为不行,结果测试一下发现大数据和小数据都测试通过了,sigh!可能这是第一题,为了吊一吊胃口吧。
代码如下:

class Solution {
public:
    vector<int> twoSum(vector<int> &numbers, int target) {
    // Start typing your C/C++ solution below
    // DO NOT write int main() function
    int i, j;
    vector<int> result;
    int n_length = numbers.size();

    for (i = 0; i < n_length - 1; i++)
        for (j = i + 1; j < n_length; j++)
            if ((numbers[i] + numbers[j]) == target)
            {
                result.push_back(i + 1);
                result.push_back(j + 1);
            }
    return result;
    }
};

3.2 hash算法

首先从左到右扫描一遍把数和坐标,存到map中。之后再扫描一遍。时间复杂度: O(N)
代码如下:

class Solution {
public:
    vector<int> twoSum(vector<int> &numbers, int target) {
    // Start typing your C/C++ solution below
    // DO NOT write int main() function
        map<int,int>tmp;
        vector<int> result;

        for (int i = 0; i < numbers.size(); i++)
            tmp[numbers[i]] = i;

        for (int i = 0; i< numbers.size(); i++)
        {
            int a = target - numbers[i];
            if (tmp.find(a) != tmp.end())
            {
                result.push_back(i + 1);
                result.push_back(tmp[a] + 1);
            }
        }

        return result;
    }
};

3.3 双指针扫描

将数组排序,然后双指针从两头向中间扫描,时间复杂度: O(NlgN)。但是这里要涉及到存储数组的下标,会有额外的存储。这里可以用一个struct解决。开始的时候想到还是像方法2的那样用map,但是当正确结果是两个同样的值,就会错误,因为只会存放最后一次的下标,故不行。
代码如下:

struct node_index
{
    int val;
    int index;
    node_index(int p_val, int p_index):val(p_val),index(p_index){}
};

static bool compare(const node_index& node1, const node_index& node2)
{
    return node1.val < node2.val;
}

class Solution {
public:
    vector<int> twoSum(vector<int> &numbers, int target) {
    // Start typing your C/C++ solution below
    // DO NOT write int main() function
        vector<int> result;
        vector<node_index> tmp;

        for (int i = 0; i < numbers.size(); i++)
            tmp.push_back(node_index(numbers[i], i));

        sort(tmp.begin(), tmp.end(), compare);

        int l = 0; int r = tmp.size() - 1;
        while (l < r)
        {
            int sum = tmp[l].val + tmp[r].val;
            if (sum == target)
            {
                if (tmp[l].index > tmp[r].index)
                {
                    result.push_back(tmp[r].index + 1);
                    result.push_back(tmp[l].index + 1);
                }
                else
                {
                    result.push_back(tmp[l].index + 1);
                    result.push_back(tmp[r].index + 1);
                }
                return result;
            }
            else if (sum < target)
                l++;
            else 
                r--;
        }
    }   
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值