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

Leetcode--Two Sum

时间:2014-07-31 20:48:17      阅读:215      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   color   os   io   for   2014   

Problem Description:

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的两个数的下标,容易想到如果是有序数组的话直接用两个变量一头一尾遍历数组即可找到所需的两个数,所以这里一个问题是如何处理无序的数的问题,能想到的是定义一个结构体,把每个元素和下标一起记录下来,然后按照元素值大小排序,,然后一头一尾遍历可以得到结果,时间复杂度主要在排序,可以达到O(nlog(n)),具体代码如下:

    
   struct node{
        int val;
        int indexs;
    };  

    bool compare(node a,node b)
    {
        return a.val<b.val;
    }    

class Solution {
public:

    vector<int> twoSum(vector<int> &numbers, int target) {
        vector<int> res;
        if(numbers.size()<2) 
            return res;
        vector<node> nums;
        for(vector<int>::size_type index=0;index!=numbers.size();++index)
        {
            node temp;
            temp.val=numbers[index];
            temp.indexs=index+1;
            nums.push_back(temp);
        }
        
        sort(nums.begin(),nums.end(),compare);
        
        vector<node>::iterator p=nums.begin();
        vector<node>::iterator q=nums.end()-1;
        while(p<q)
        {
            if((p->val+q->val)>target)
                q--;
            else 
            if((p->val+q->val)<target)
                p++;
            else
            {
                if(p->indexs<q->indexs)
                {
                    res.push_back(p->indexs);
                    res.push_back(q->indexs);
                }
                else
                {
                    res.push_back(q->indexs);
                    res.push_back(p->indexs);
                }
                break;
            }
        }
        return res;
    }
};

在discuss中看到有利用hash_map的性质来做的,实质就是利用map记录下每个元素的下标,然后固定一个元素查找另一个元素是否存在,时间复杂度可以达到O(n),具体实现如下: 

class Solution {
public:
    vector<int> twoSum(vector<int> &numbers, int target) {
        vector<int> res;
        if(numbers.size()<2)
            return res;
        unordered_map<int,int> numsmap;
        
        for(vector<int>::size_type index=0;index!=numbers.size();++index)
            numsmap[numbers[index]]=index;
        unordered_map<int,int>::iterator flag=numsmap.end();
        
        for(vector<int>::size_type index=0;index!=numbers.size();++index)
        {
            int temp=target-numbers[index];
            flag=numsmap.find(temp);
            if(flag!=numsmap.end()&&flag->second!=index)
            {
                if(index<flag->second)
                {
                    res.push_back(index+1);
                    res.push_back(flag->second+1);
                }
                else
                {
                    res.push_back(flag->second+1);
                    res.push_back(index+1);
                }
                break;
            }
        }
        return res;
    }
};


Leetcode--Two Sum,布布扣,bubuko.com

Leetcode--Two Sum

标签:des   style   blog   color   os   io   for   2014   

原文地址:http://blog.csdn.net/longhopefor/article/details/38322565

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