1. 程式人生 > >leetcode解題之 15. 3Sum Java版(結果為目標值的三個數字)

leetcode解題之 15. 3Sum Java版(結果為目標值的三個數字)

15. 3Sum

Given an array S of n integers, are there elementsa, b, c 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]
]

先對陣列進行排序,時間複雜度O(log(n)),然後定好一個數的位置,查詢另外兩個數的和等於-nums[i]的組合,由於陣列排好序了,所以可以從兩邊往中間走,當結果大於0的時候後邊往後退一步,否則前邊進一步,時間複雜度O(n^2),所以時間複雜度為O(n^2)

	// 避免重複!!!!
	// 避免重複!!!!
	// 避免重複!!!!
	public List<List<Integer>> threeSum(int[] nums) {
		List<List<Integer>> ret = new ArrayList<>();

		if (nums == null || nums.length < 3)
			return ret;
		int len = nums.length;
		Arrays.sort(nums);
		// 注意,對於 num[i],尋找另外兩個數時,只要從 i+1 開始找就可以了。
		// 這種寫法,可以避免結果集中有重複,因為陣列時排好序的,
		//所以當一個數被放到結果集中的時候,其後面和它相等的直接被跳過。
		for (int i = 0; i < len; i++) {
			// 可省,目的減少無意義的迴圈
			if (nums[i] > 0)
				break;
			// 避免重複!!!!
			if (i > 0 && nums[i] == nums[i - 1])
				continue;
			// 往後找,避免重複
			int begin = i + 1;
			int end = len - 1;
			while (begin < end) {
				int sum = nums[i] + nums[begin] + nums[end];
				if (sum == 0) {
					List<Integer> list = new ArrayList<>();
					list.add(nums[i]);
					list.add(nums[begin]);
					list.add(nums[end]);
					ret.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--;
				else
					begin++;
			}
		}
		return ret;
	}