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

  题意是从一堆无序的数中,找到两个数,使其和等于给定值。分析可以知道,有几种方法,既然是无序的,可以考虑先排序,然后用双指针从头到尾移动,知道找到合适的两个数,这要注意对下标的保存,因为题目要求返回的是下标;

  第二种方法,不用排序,对于给定的值i,找数组中是否存在另外一个值target - i,存在则返回这两个值的下标,这要借助于hashTable来O(1)的查找。

 第三种方法就是,直接暴力,枚举所有的两个数的和的可能性,这样时间复杂度很高,达到了O(N^2)

直接查找:

class Solution {
public:
    //直接查找
    vector<int> twoSum(vector<int>& nums, int target) {
        unsigned numLen = nums.size();
        vector<int> ans;
        if(numLen > 1)
        {
            unordered_map<int, set<int>> hmap;
            for(unsigned i = 0; i < numLen; ++i)
            {
                hmap[nums[i]].insert(i + 1);
            }
            
            int tmpTarget;
            for(unsigned i = 0; i < numLen; ++i)
            {
                tmpTarget = target - nums[i];
                if(tmpTarget == nums[i] && hmap[tmpTarget].size() >= 2)
                {
                    ans.push_back(*(hmap[nums[i]].begin()));
                    hmap[nums[i]].erase(hmap[nums[i]].begin());
                    ans.push_back(*(hmap[nums[i]].begin()));
                    return ans;
                }
                else if(tmpTarget != nums[i] && hmap.find(tmpTarget) != hmap.end())
                {
                    ans.push_back(i + 1);
                    ans.push_back(*(hmap[tmpTarget].begin()));
                    return ans;
                }
            }
        }
        
        return ans;
    }
};

排序+双指针

typedef struct Node
{
    int val;
    int index;
    Node(int v, int idx) : val(v), index(idx) {}
}Node;

bool comp(const Node &a, const Node &b)
{
    if(a.val == b.val)
        return a.index < b.index;
    return a.val < b.val;
}

class Solution {
public:
    vector<int> twoSum(vector<int>& nums, int target) {
        int numLen = nums.size();
        vector<int> ans;
        if(numLen > 1)
        {
            vector<Node> NodeVec;
            for(unsigned i = 0; i < numLen; ++i)
            {
                NodeVec.push_back(Node(nums[i], i + 1));
            }
            
            sort(NodeVec.begin(), NodeVec.end(), comp);
            int low = 0, high = numLen - 1, tmpSum = 0;
            while(low < high)
            {
                tmpSum = NodeVec[low].val + NodeVec[high].val;
                if(tmpSum == target)
                {
                    ans.push_back((NodeVec[low].index < NodeVec[high].index ? NodeVec[low].index : NodeVec[high].index));
                    ans.push_back((NodeVec[low].index > NodeVec[high].index ? NodeVec[low].index : NodeVec[high].index));
                    return ans;
                }
                else if(tmpSum < target)
                {
                    ++low;
                }
                else
                    --high;
            }
        }
        
        return ans;
    }
};


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值