15. 3Sum

给你一个含有n个整数的数组S,找出S中满足a+b+c=0的元素a,b,c。要求它们不能重复。

1. 枚举元素(超时)

	public List<List<Integer>> threeSum(int[] nums)
	{
		List<List<Integer>> list = new ArrayList<List<Integer>>();
		List<Integer> element = null;
		Arrays.sort(nums); // 先对数据nums由小到大排序
		for (int i=0; i<nums.length; i++)
		{
			for (int j=i+1; j<nums.length; j++)
			{
				for (int k=j+1; k<nums.length; k++)
				{
					// 如果满足和为0的条件
					if (nums[i] + nums[j] + nums[k] == 0)
					{
						element = new ArrayList<>();
						element.add(nums[i]);
						element.add(nums[j]);
						element.add(nums[k]);
						// 如果该元素之前没有出现过
						if (!list.contains(element))
						{
							list.add(element);
						}
					}
				}
			}
		}
		return list;
	}

2. 先求所有的组合数,在从组合数中筛选(超时)

List<List<Integer>> all = new ArrayList<List<Integer>>(); // 存放所有组合数的列表
	List<Integer> list = null; // 存放每一轮的组合数
	int[] a = new int[100]; // 默认值是0 
	
	// 组合问题(从n个数中任取三个数)超时
	public List<List<Integer>> threeSum(int[] nums)
	{
		// 先对数组nums进行判断
		if (nums == null || nums.length < 3)
		{
			return all;
		}
		// 生成所有的3位数的组合数
		Arrays.sort(nums);  // 排序
		com(nums, nums.length, 3);
		return all;
	}
	/**
	 * 从任意的n个数中任取r个数
	 */
	void com(int[] nums, int n, int r) // 超时
	{
		int i=0, j=0;
		for (i=n; i>=r; i--)
		{
			a[r] = nums[i-1]; // 注意数组下标
			if (r == 1)
			{
				int sum = 0; // 统计和
				list = new ArrayList<Integer>(); // 记录本轮的组合数
				for (j=1; j<=3; j++) // 这里3是要取的数的个数
				{
					list.add(a[j]);
				}
				// 检查元素之和是否为0
				for (int k=0; k<list.size(); k++)
				{
					sum += list.get(k);
				}
				if (sum == 0)
				{
					if (!all.contains(list))
						all.add(list);
				}
			}
			else
			{
				// 从余下的n-1个数中取r-1个数的组合
				com(nums, i-1, r-1);
			}
		}
	}

3.  网上的思路

从前向后遍历数组中每个元素,遇到重复的就过滤掉。每取到一个nums[i],从从它后面开始二分查找,找满足

要求的另外两个加数,也要注意过滤掉重复的元素。

public List<List<Integer>> threeSum(int[] nums)
	{
		int sum = 0; // 元素之和
		List<List<Integer>> all = new ArrayList<>(); // 存放最终的结果
		List<Integer> list = null; // 存放每一个符合条件的三元组
		// 先对数组的情况进行判断
		if (nums == null || nums.length < 3)
		{
			return all;
		}
		// 排序
		Arrays.sort(nums);
		for (int i = 0; i < nums.length - 2; i++)
		{
			// 在数组有序的情况下,第一个加数比0大,跳出循环
			if (nums[i] > 0)
				break;
			// 跳过重复元素
			if (i > 0 && nums[i] == nums[i - 1])
				continue;

			int begin = i + 1; // 查找的左边界
			int end = nums.length - 1; // 查找的右边界
			// 二分查找
			while (begin < end)
			{
				sum = nums[i] + nums[begin] + nums[end];
				// 符合条件
				if (sum == 0)
				{
					list = new ArrayList<>();
					list.add(nums[i]);
					list.add(nums[begin]);
					list.add(nums[end]);
					all.add(list);

					begin++;
					end--;

					// 跳过重复元素
					while (begin < end && nums[begin] == nums[begin - 1])
						begin++;
					while (begin < end && nums[end] == nums[end + 1])
						end--;
				} else if (sum > 0)
				{
					end--; // 向前找
					while (begin < end && nums[end] == nums[end + 1])
						end--;
				} else
				{
					begin++; // 向后找
					while (begin < end && nums[begin] == nums[begin - 1])
						begin++;
				}
			}
		}
		return all;
	}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值