LeetCode-----第十五题-----三数之和

三数之和

难度:中等

给你一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?请你找出所有满足条件且不重复的三元组。

注意:答案中不可以包含重复的三元组。

 

示例:

给定数组 nums = [-1, 0, 1, 2, -1, -4],

满足要求的三元组集合为:
[
  [-1, 0, 1],
  [-1, -1, 2]
]

题目分析:

       这里主要使用排序+双指针,主要思路就是固定一个数不动,然后使用双指针找数求和。这里还有一种想法就是双指针固定,然后使用单指针进行二分法查找。(这个想法弊端就是双指针的移动在找到符合条件的数之后无法确定左右指针哪个移动,前面那种解法的话,找到符合条件的数就双指针一起向中间移动,但是后者不行,双指针移动之后,后面就不能再用到这个数了,而前者的index指针还会指到后面的数,所以不会发生遗漏的现象

 

参考代码:

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <stack>
#include <queue>
#include <list>
#include <unordered_map>
#include <cstring>
#include <map>
#include <stdexcept>

using namespace std;

class Solution {
public:
	vector<vector<int>> threeSum(vector<int>& nums) {
		vector<vector<int>> res;
		if (nums.empty())
			return res;

		vector<int> temp;
		int size = (int)nums.size();
		//在使用双指针之前,首先进行快排(从小到大)
		sort(nums.begin(), nums.end());
		//固定前面一个值不变,后边使用双指针
		for (int index = 0; index < size; ++index)
		{
			//如果最前面的值大于0,那么后面的值肯定也大于0,所以可以结束循环了
			//这里要注意的是,可能会发生[0,0,0]的情况,所以这里不能等于0
			if (nums[index] > 0)
				break;
			//初始化双指针
			int left = index + 1;
			int right = size - 1;
			while (left < right)
			{
				//进行一个变量赋值,后边用作双指针的移动
				int l_num = nums[left];
				int r_num = nums[right];
				//判断三种情况之一的相等情况
				if ((nums[index] + nums[left] + nums[right]) == 0)
				{
					//相等之后直接压入结果中
					temp.push_back(nums[index]);
					temp.push_back(nums[left]);
					temp.push_back(nums[right]);
					res.push_back(temp);
					//这里不要忘记把临时存储清空,否者会一直存储之前的数据
					temp.clear();
					//去重,并且去完之后进行指针移动
					while (left < right && nums[left] == l_num)++left;
					while (left < right && nums[right] == r_num)--right;
				}//判断三种情况之一的三数之和大于0情况
				else if ((nums[index] + nums[left] + nums[right]) > 0)
				{
					--right;
				}//判断三种情况之一的三数之和小于0情况
				else if ((nums[index] + nums[left] + nums[right]) < 0)
				{
					++left;
				}
			}//不要忘记index也是需要去重的
			while ((index+1)<size && nums[index] == nums[index+1])
			{
				index++;
			}
		}
		return res;
	}
};

int main()
{
	Solution solution;
	vector<int> arr = { 0,0,0};
//	vector<int> arr = {-4, -2, -2, -2, 0, 1, 2, 2, 2, 3, 3, 4, 4, 6, 6};
	vector<vector<int>> res;

	res = solution.threeSum(arr);

	if(!res.empty())
	for (int i = 0; i < res.size(); i++)
	{
		for (int j = 0; j < 3; j++)
		{
			cout << res[i][j];
		}
		cout << endl;
	}

	system("pause");
	return 0;
}

 

第二种算法(有问题)

class Solution {
public:
	vector<vector<int>> threeSum(vector<int>& nums) {
		vector<vector<int> > res;
		if (nums.size() < 3)
			return res;

		vector<int> temp;
		unordered_map<string, int> my_hap;
		int left = 0;
		int l_i = left+1;
		int right = nums.size() - 1;
		int r_j = right-1;
		int mid = left + (right - left) / 2;
		
		sort(nums.begin(),nums.end());

		while (left < right && !(nums[left] < 0 && nums[right] < 0)
				&& !(nums[left] > 0 && nums[right] > 0))
		{
			while (l_i <= r_j)
			{
				if ((nums[right] + nums[left] + nums[mid]) < 0)
				{
					l_i = mid + 1;
					mid = l_i + (r_j - l_i) / 2;
				}
				else if ((nums[right] + nums[left] + nums[mid]) > 0)
				{
					r_j = mid - 1;
					mid = l_i + (r_j - l_i) / 2;
				}
				else {
					string str = to_string(nums[left]);
					str += to_string(nums[mid]);
					str += to_string(nums[right]);
					if (my_hap.count(str) > 0)
						break;
					my_hap[str] = 1;
					temp.push_back(nums[left]);
					temp.push_back(nums[mid]);
					temp.push_back(nums[right]);
					res.push_back(temp);
					temp.clear();
					break;
				}
			}
			if ((nums[left] + nums[right]) > 0)
				--right;
			else if((nums[left] + nums[right]) < 0)
				++left;
			else
			{
				while (left < right && nums[left] == nums[left+1]) ++left;
				while (left < right && nums[right] == nums[right - 1]) --right;
			}
			l_i = left + 1;
			r_j = right - 1;
			mid = l_i + (r_j - l_i) / 2;
		}
		return res;
	}
};

参考代码(自己写的):

class Solution {
public:
	vector<vector<int>> threeSum(vector<int>& nums) {
		vector<vector<int>> res;

		if (nums.size() < 3)
			return res;

		int pmove = 0;
		int left = 1;
		int right = nums.size() - 1;
		sort(nums.begin(), nums.end());

		for (int i = 0; i < nums.size() - 2; i++)
		{
			left = i + 1;
			right = nums.size() - 1;
			while (left < right)
			{
				if ((nums[i] + nums[left] + nums[right]) < 0)
				{
					while (left + 1 < right && nums[left] == nums[left+1]) left++;
					left++;
				}
				else if ((nums[i] + nums[left] + nums[right]) > 0){
					while (right - 1 > left && nums[right] == nums[right - 1]) right--;
					right--;
				}
				else{
					vector<int> temp;
					temp.push_back(nums[i]);
					temp.push_back(nums[left]);
					temp.push_back(nums[right]);
					res.push_back(temp);
					while (left + 1 < right && nums[left] == nums[left+1]) left++;
					left++;
					while (right - 1 > left && nums[right] == nums[right-1]) right--;
					right--;
				}
			}
			while (i + 1 < left && nums[i] == nums[i + 1]) i++;
		}
		return res;
	}
};

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值