码迷,mamicode.com
首页 > 其他好文 > 详细

Leetcode-Two Sum

时间:2015-08-02 18:20:21      阅读:112      评论:0      收藏:0      [点我收藏+]

标签:two-sum   leetcode   

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;
    }
};

版权声明:本文为博主原创文章,未经博主允许不得转载。

Leetcode-Two Sum

标签:two-sum   leetcode   

原文地址:http://blog.csdn.net/xietingcandice/article/details/47208697

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!