leetcode——TwoSum、3Sum

TwoSum

Given an array of integers, return indices of the two numbers such that they add up to a specific target.

You may assume that each input would have exactly one solution.

Example:

Given nums = [2, 7, 11, 15], target = 9,

Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].

解析:寻找序列中唯一的和为目标数的两个元素,并返回下标。做法一是直接暴力搜索,但是时间效率低下。另一种可行的做法是先将元素排序,然后用两个指针指向头尾,通过改变指针去寻找目标元素。由于需要返回下标,因此可以先用pair存储元素与其初始下标。具体实现代码如下,时间复杂度为O(nlogn):

class Solution {
public:
	static bool cmp(pair<int, int>& a, pair<int, int>& b)
	{
		return a.first < b.first;
	}
    vector<int> twoSum(vector<int>& nums, int target) {
        vector<int> v;
        vector<pair<int, int> >	num;
        int n = nums.size(), j = 0, k = n - 1;
        for(int i=0; i<n; i++)
        	num.push_back(make_pair(nums[i], i));
        sort(num.begin(), num.end(), cmp);
        while(j < k)
        {
        	if(num[j].first + num[k].first < target)	j ++;
        	else if(num[j].first + num[k].first > target)	k --;
        	else
        	{
        		v.push_back(num[j].second);
        		v.push_back(num[k].second);
        		break;
        	}
		}
		return v;
    }
};

还有一种实现比较简易,利用map容器存储元素,之后只需要对每个元素nums[i]判断是否存元素target-nums[i]即可,代码如下:

class Solution {
public:
    vector<int> twoSum(vector<int>& nums, int target) {
        vector<int> ans;
        map<int, int> M;
        int n = nums.size();
        for(int i=0; i<n; i++)
        	M[nums[i]] = i+1;
        for(int i=0; i<n; i++)
        	if(M[target - nums[i]] != i+1 && M[target - nums[i]] > 0)
        	{
        		ans.push_back(i);
        		ans.push_back(M[target - nums[i]]-1);
        		break;
			}
		return ans;
    }
};


3Sum

Given an array S of n integers, are there elements abc in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.

Note: 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]
]

解析:根据TwoSum可以很容易得到3Sum的解法,排序之后,对每个元素nums[i],在序列nums[i+1]到nums[n-1]中寻找和为0-nums[i]的两个元素。具体代码如下,时间复杂度为O(n^2):

class Solution {
public:
    vector<vector<int> > threeSum(vector<int>& nums) {
        vector<vector<int> > ans;
        int n = nums.size(), m = -1;
        sort(nums.begin(), nums.end());
        for(int i=0; i<n; i++)
        	if(i == 0 || nums[i] != nums[i-1])
        	{
        		int j = i + 1, k = n - 1;
	        	while(j < k)
		        {
		        	if(nums[i] + nums[j] + nums[k] < 0)	j ++;
		        	else if(nums[i] + nums[j] + nums[k] > 0)	k --;
		        	else
		        	{
						while(j+1 < k && nums[j+1]==nums[j])	j ++;
						while(j < k-1 && nums[k-1]==nums[k])	k --;
		        		vector<int> v;
			        	v.push_back(nums[i]);
			        	v.push_back(nums[j]);
			        	v.push_back(nums[k]);
		        		ans.push_back(v);
		        		j ++;
		        		k --;
		        	}
				}
			}
		return ans;
    }
};




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值