Two Sum

初阶。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


思路:

此题需要返回的是下标,不是数字本身。

思路1。暴力,时间复杂度o(n^2)

思路2。用一个hash表,存储每个数对应的下标,复杂度O(n).

class Solution{
public:
	vector<int> twoSum(vector<int> &num,int target)
	{
		unordered_map<int,int> mapping;
		vector<int> result;

		for(unsigned int i=0;i<num.size();i++)
		{
			mapping[num[i]] = i;
		}

		for(unsigned int i=0;i<num.size();i++)
		{
			const int gap = target - num[i];
			if (mapping.find(gap)!=mapping.end() && mapping[gap] > i)
			{
				result.push_back(i+1);
				result.push_back(mapping[gap]+1);
				break;
			}
		}
		return result;
	}
};


进阶。3Sum

Given an array S of n integers, are there elements a; b; c in S such that a + b + c = 0? Find all unique
triplets in the array which gives the sum of zero.
Note:
?Elements in a triplet (a; b; c) must be in non-descending order. (ie, a b c)
?The solution set must not contain duplicate triplets.
For example, given array S = {-1 0 1 2 -1 -4}.
A solution set is:
(-1, 0, 1)
(-1, -1, 2)

思路:先排序,然后左右夹逼。复杂度O(n^2)

class Solution{
public:
	vector<vector<int>> threeNum(vector<int> &num)
	{
		vector<vector<int>> result;
		if (num.size() < 3) return result;
		sort(num.begin(),num.end());
		int target = 0;
		vector<int> rec;

		auto last = num.end();
		for(auto a = num.begin();a < prev(last,2);a++)
		{
			auto b = next(a);
			auto c = prev(last);
			while(b<c)
			{
				if(*a + *b + *c < target)
				{
					++b;
				}else if(*a + *b + *c > target)
				{
					--c;
				}
				else{
					//result.push_back({*a, *b, *c});
					rec.clear();
					rec.push_back(*a);
					rec.push_back(*b);
					rec.push_back(*c);
					result.push_back(rec);
					++b;
					--c;
				}
			}
		}
		sort(result.begin(),result.end());
		result.erase(unique(result.begin(),result.end()),result.end());
		return result;
	}
};


进阶+.3Sum Closest

Given an array S of n integers, find three integers in S such that the sum is closest to a given number,
target. Return the sum of the three integers. You may assume that each input would have exactly one solution.
For example, given array S = {-1 2 1 -4}, and target = 1.
The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).

思路。同上,先排序,然后再左右夹逼。复杂度O(n^2).


进阶++。4Sum

Given an array S of n integers, are there elements a; b; c, and d in S such that a+b+c+d = target?
Find all unique quadruplets in the array which gives the sum of target.
Note:
?Elements in a quadruplet (a; b; c; d) must be in non-descending order. (ie, a b c d)
?The solution set must not contain duplicate quadruplets.
For example, given array S = {1 0 -1 0 -2 2}, and target = 0.
A solution set is:
(-1, 0, 0, 1)
(-2, -1, 1, 2)
(-2, 0, 0, 2)

思路。

思路1,先排序,然后左右夹逼。复杂度O(n^3).

思路2,用一个hashmap先缓存两个数的和,时间复杂度,平均O(n^2),最坏o(n^4),空间复杂度O(n^2).

思路3,用multimap.时间复杂度O(n^2),空间复杂度O(n^2).







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值