leetcode挨个儿刷150428(3):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的值。

可能出现的坑
- vector的长度是一个坑,考虑为0为1的情况。
- 数组中可能会出现两个一样的数,比如numbers=[0,1,4,5,0] ;target=0这种情况。如果使用map以value为key存储的话,只能保存一个。map不支持多个键值。

以下是我的两种做法,均AC。
第一种方法使用了map,用来发现当前探索的值的另一半(target-current)是否存储了。若已经存储,则返回。若为存储,则加入map。复杂度为O(n)。代码如下:

class Solution {
public:
    vector<int> twoSum(vector<int>& nums, int target) {
        vector<int> index;
        if( (nums.size() == 0) || (nums.size() == 1) )
            return index;
        map<int,int> value_index;
        for(int i = 0;i < nums.size();i++)
        {
            int current_value = nums[i];
            if(value_index.count(target - current_value) == 1)
            {
                index.push_back(value_index[target - current_value] + 1);
                index.push_back(i+1);
                return index;
            }
            else
            {
                value_index[current_value] = i;
            }
        }
        return index;
    }
};

第二种方法使用了结构体,并对结构体数组进行排序。从两边到中间的进行查找。时间复杂度主要用于排序,复杂度为O(nlogn)。代码如下:

struct value_index
{
    int value;
    int index;
};
bool compare(value_index a,value_index b)
{
    return a.value  < b.value;
}

class Solution {
public:
    vector<int> twoSum(vector<int>& nums, int target) {
        vector<int> index;
        if( nums.size() <= 1 )
            return index;
        vector<value_index> value_index_array(nums.size());
        for(int i = 0; i < nums.size();i++)
        {
            value_index_array[i].value = nums[i];
            value_index_array[i].index = i;
        }
        sort(value_index_array.begin(),value_index_array.end(),compare);
        int begin_index = 0;
        int end_index = value_index_array.size() - 1;
        while(begin_index != end_index)
        {
            if(value_index_array[begin_index].value + value_index_array[end_index].value < target)
            {
                begin_index++;
            }
            else if(value_index_array[begin_index].value + value_index_array[end_index].value > target)
            {
                end_index--;
            }
            else
            {
                int index1 = value_index_array[begin_index].index;
                int index2 = value_index_array[end_index].index; 
                int bigger = index1 > index2 ? index1 : index2;
                int smaller = index1 + index2 - bigger;
                index.push_back(smaller + 1);
                index.push_back(bigger + 1);
                return index;
            }
        }
        return index;
    }    
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值