Leetcode-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
题目比较简单,就是找到两个数相加的值等于target有很多做法
1 遍历两次数组,那么时间复杂度就是o(n2)了
2 因为只需要找到一组数据,可以现将数据进行排序,然后两个指针向中间进行查找,但是这里就必须考虑排序的时间复杂度了,查找的过程基本就是小于一遍的遍历过程
3 通过map,每找到一个值,直接计算差值,然后查找,这里的时间复杂度时对数时间,可以利用hash_map等数据,查找时间降低常熟时间,但是在计算 hash值的过程中还是有时间消耗的,同时如果hash表的大小比较小,那么可能引起很大的时间开销
4 根据dfs进行查找,那么这种方法,就不一定非要是两个数据,多个数据的和也是可以的

利用第二种方法的实现情况

struct Node
{
    int num, pos;
};
bool cmp(Node a, Node b)
{
    return a.num < b.num;
}
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> array;
        for (int i = 0; i < numbers.size(); i++)
        {
            Node temp;
            temp.num = numbers[i];
            temp.pos = i;
            array.push_back(temp);
        }

        sort(array.begin(), array.end(), cmp);
        for (int i = 0, j = array.size() - 1; i != j;)
        {
            int sum = array[i].num + array[j].num;
            if (sum == target)
            {
                if (array[i].pos < array[j].pos)
                {
                    result.push_back(array[i].pos + 1);
                    result.push_back(array[j].pos + 1);
                } else
                {
                    result.push_back(array[j].pos + 1);
                    result.push_back(array[i].pos + 1);
                }
                break;
            } else if (sum < target)
            {
                i++;
            } else if (sum > target)
            {
                j--;
            }
        }
        return result;
    }
};

利用dfs的方法的实现情况,但是时间复杂度太高,会导致超时

class Solution {
public:
    vector<int> twoSum(vector<int>& nums, int target) {
        vector<int> ret;
        vector<vector<int>> temp;
        if(nums.empty())
        {
            return ret;
        }
        GetPath(nums,0,0,ret,temp,target);
        if(!temp.empty())
        {
            return temp[0];
        }
        else
        {
            return ret;
        }
    }
    void GetPath(vector<int>& nums,int index,int sum, vector<int>& ret,vector<vector<int>>&temp,int target)
    {
        if(sum > target)
        {
            return;
        }
        if(sum+nums[index] == target)
        {
            temp.push_back(ret);
            return;
        }
        for(;index < nums.size();index++)
        {
            ret.push_back(nums[index+1]);
            GetPath(nums,index+1,sum+nums[index],ret,temp,target);
            ret.pop_back();
        }
        return;
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值