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,从数组中要找出a+b=target。等式转换一下,我们可以知道,a=target-b。所以我们要做的事情可以翻译为,找到一个数a,满足a与target-a都在数组之中。

第一个思路,遍历数组中的某一个数,对于每个数再一次遍历数组中的所有数,找到满足条件的两个数。这个算法的时间复杂度为O(n2),空间复杂度为O(1)。
第二个思路,在前一个算法的基础上降低时间复杂度。我们可以将数组排序,然后定义两个指针,一个指针从左向右,另一个从右向左,遍历直到找到满足条件的两个数。由于排序的最佳时间复杂度为O(nlogn),两个指针的遍历时间复杂度为O(n)。所以总的时间复杂度为O(nlogn)
第三个思路,我希望通过O(n)的时间复杂度完成要求。第一遍O(n)的算法将每个数据a对应的target-a建立查询的数据结构,例如Hash表;第二遍遍历时,查询每个数是否在Hash表中,每次查询时间复杂度为O(1),总的时间复杂度是O(n)。但是Hash表将需要一定的存储空间,为了节省空间,我们可以采用bitmap的方法来最大化的压缩空间复杂度。

代码:

#include <cmath>


class bitmap
{
public:
    bitmap(int n);
~bitmap();
bool set(int k);
bool val(int k);

private:
int len;
unsigned int* map;
};


bitmap::bitmap(int n)
{
        len = n/32+1;
map = new unsigned int[len];


for(int i = 0; i < len; i++)
{
map[i] = 0;
}
}


bitmap::~bitmap()
{
        delete map;
        len = 0;
}


bool bitmap::set(int k)
{
int a = k/32;
int b = k%32;


if(a >= len)
{
return false;
}
else
{
int tmp = map[a]/pow(2,b);
if(tmp%2 == 0)
{
map[a] += pow(2,b);
}
return true;
}
}


bool bitmap::val(int k)
{
int a = k/32;
int b = k%32;


if(a >= len)
{
return false;
}
else
{
int tmp = map[a]/pow(2,b);
if(tmp%2 == 1)
{
return true;
}
else
{
return false;
}
}
}




class Solution {
public:
    vector<int> twoSum(vector<int> &numbers, int target) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        bitmap* bm = new bitmap(target);
        for(int i = 0;i < numbers.size(); i++)
        {
            bm->set(target - numbers[i]);
        }


        for(int i = 0;i < numbers.size(); i++)
        {
            if(bm->val(numbers[i]))
            {
                for(int j = i + 1;j < numbers.size(); j++)
                {
                    if(numbers[i] == target - numbers[j])
                    {
                        vector<int> v;
                        v.push_back(i+1);
                        v.push_back(j+1);
                        return v;
                    }
                }
            }
        }
    }
};


  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值