LeetCode15. 三数之和和LeetCode1.两数之和

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/Miaoshuowen/article/details/102651917

LeetCode1. 两数之和
给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。

你可以假设每种输入只会对应一个答案。但是,你不能重复利用这个数组中同样的元素。

示例:

给定 nums = [2, 7, 11, 15], target = 9

因为 nums[0] + nums[1] = 2 + 7 = 9
所以返回 [0, 1]

思路1:
暴力遍历,双重循环,时间复杂度O(N^2);
思路2:
HashMap存值法,时间复杂度为O(N)
代码:

public class Twosum {
	public int[] twoSum(int[] nums, int target) {
		int length = nums.length;
		int[] result = new int[2];

		HashMap<Integer, Integer> map = new HashMap<>();
		for (int i = 0; i < length; i++) {
			if (map.containsKey(target - nums[i])) {
				result[0] = map.get(target - nums[i]);
				result[1] = i;
				return result;
			}
			map.put(nums[i], i);
		}
		return result;
	}
}

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

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

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

满足要求的三元组集合为:

[
  [-1, 0, 1],
  [-1, -1, 2]
]

思路1:
暴力法搜索为 O(N^3) 时间复杂度,可通过双指针动态消去无效解来优化效率。
思路2:
HashMap存值法:
思路3:
双指针法思路: 先将给定 nums 排序,复杂度为 O(NlogN)。 固定 3 个指针中最左(最小)数字的指针 k,双指针 i,j 分设在数组索引 (k,len(nums)) 两端,通过双指针交替向中间移动,记录对于每个固定指针 k 的所有满足 nums[k] + nums[i] + nums[j] == 0 的 i,j 组合:

当 nums[k] > 0 时直接break跳出:因为 nums[j] >= nums[i] >= nums[k] > 0,即 3 个数字都大于 000 ,在此固定指针 k 之后不可能再找到结果了。

当 k > 0且nums[k] == nums[k - 1]时即跳过此元素nums[k]:因为已经将 nums[k - 1] 的所有组合加入到结果中,本次双指针搜索只会得到重复组合。

i,j 分设在数组索引 (k,len(nums)) 两端,当i < j时循环计算s = nums[k] + nums[i] + nums[j],并按照以下规则执行双指针移动:

当s < 0时,i += 1并跳过所有重复的nums[i];
当s > 0时,j -= 1并跳过所有重复的nums[j];
当s == 0时,记录组合[k, i, j]至res,执行i += 1和j -= 1并跳过所有重复的nums[i]和nums[j],防止记录到重复组合。

复杂度分析:

时间复杂度 O(N^2)其中固定指针k循环复杂度 O(N),双指针 i,j 复杂度 O(N)。
空间复杂度 O(1)指针使用常数大小的额外空间。

代码:

public class Sumthree15 {

	public static void main(String[] args) {
		// TODO 自动生成的方法存根
		int[] nums = { -2, 0, 1, 1, 2 };
		Sumthree15 test = new Sumthree15();
		System.out.println(test.threeSum(nums));
	}

	public List<List<Integer>> threeSum(int[] nums) {
		Arrays.sort(nums);
		List<List<Integer>> result = new ArrayList<List<Integer>>();

		for (int k = 0; k < nums.length - 2; k++) {
			if (nums[k] > 0) {
				break;
			}
			int i = k + 1;
			if (k > 0 && nums[k] == nums[k - 1]) {
				continue;
			}
			int j = nums.length - 1;
			int s = nums[k] + nums[i] + nums[j];

			while (i < j) {
				if (s < 0) {
					i++;
					while (i < j && nums[i] == nums[i - 1]) {
						i++;
					}
					s = nums[k] + nums[i] + nums[j];
				} else if (s > 0) {
					j--;
					while (i < j && nums[j] == nums[j + 1]) {
						j--;
					}
					s = nums[k] + nums[i] + nums[j];
				} else {
					List<Integer> now = new ArrayList<Integer>();
					now.add(nums[k]);
					now.add(nums[i]);
					now.add(nums[j]);
					result.add(now);
					i++;
					j--;
					while (i < j && nums[i] == nums[i - 1]) {
						i++;
					}
					while (i < j && nums[j] == nums[j + 1]) {
						j--;
					}
					s = nums[k] + nums[i] + nums[j];
				}
			}
		}
		return result;
	}
}

猜你喜欢

转载自blog.csdn.net/Miaoshuowen/article/details/102651917
今日推荐