项目作者: eMahtab

项目描述 :
Three Sum
高级语言:
项目地址: git://github.com/eMahtab/three-sum.git
创建时间: 2019-11-27T17:06:38Z
项目社区:https://github.com/eMahtab/three-sum

开源协议:

下载


Three Sum

https://leetcode.com/problems/3sum

Given an array nums of n integers, are there elements a, b, c in nums 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.

  1. Example:
  2. Given array nums = [-1, 0, 1, 2, -1, -4],
  3. A solution set is:
  4. [
  5. [-1, 0, 1],
  6. [-1, -1, 2]
  7. ]

Implementation 1 : O(n^3) Time Limit Exceeded 😰

  1. public static List<List<Integer>> threeSum(int[] nums) {
  2. List<List<Integer>> result = new ArrayList<List<Integer>>();
  3. if(nums == null || nums.length < 3)
  4. return result;
  5. int n = nums.length;
  6. for(int i = 0; i < n - 2; i++) {
  7. for(int j = i + 1; j < n - 1; j++) {
  8. for(int k = j + 1; k < n; k++) {
  9. if((nums[i] + nums[j] + nums[k]) == 0 ) {
  10. addResult(result, new Integer[] {nums[i], nums[j], nums[k]});
  11. }
  12. }
  13. }
  14. }
  15. return result;
  16. }
  17. private static void addResult(List<List<Integer>> result, Integer[] triplet) {
  18. Set<Integer> threeSum = new HashSet<Integer>(Arrays.asList(triplet));
  19. boolean alredayExists = false;
  20. for(List<Integer> list: result) {
  21. Set<Integer> res = new HashSet<Integer>(list);
  22. if(res.equals(threeSum)) {
  23. alredayExists = true;
  24. break;
  25. }
  26. }
  27. if(!alredayExists) {
  28. result.add(Arrays.asList(triplet));
  29. }
  30. }

Above implementation have Runtime complexity of O(n^3) and space complexity of O(1)

  1. Runtime Complexity = O(n^3)
  2. Space Complexity = O(1)

Implementation 2 : O(n^2) Time Limit Exceeded 😰

  1. public static List<Integer[]> threeSum(int[] nums) {
  2. List<Integer[]> result = new ArrayList<Integer[]>();
  3. if(nums == null || nums.length < 3)
  4. return result;
  5. int n = nums.length;
  6. Arrays.sort(nums);
  7. for(int i = 0; i < n - 2; i++) {
  8. int left = i + 1;
  9. int right = n - 1;
  10. while(left < right) {
  11. if( (nums[i] + nums[left] + nums[right]) > 0) {
  12. right --;
  13. } else if( (nums[i] + nums[left] + nums[right]) < 0) {
  14. left++;
  15. } else {
  16. addResult(result, new Integer[] {nums[i], nums[left], nums[right]});
  17. left++;
  18. right--;
  19. }
  20. }
  21. }
  22. return result;
  23. }
  24. private static void addResult(List<Integer[]> result, Integer[] triplet) {
  25. Set<Integer> threeSum = new HashSet<Integer>(Arrays.asList(triplet));
  26. boolean alredayExists = false;
  27. for(Integer[] list: result) {
  28. Set<Integer> res = new HashSet<Integer>(Arrays.asList(list));
  29. if(res.equals(threeSum)) {
  30. alredayExists = true;
  31. break;
  32. }
  33. }
  34. if(!alredayExists) {
  35. result.add(triplet);
  36. }
  37. }

Above implementation have Runtime complexity of O(n^2) and space complexity of O(1)

  1. Runtime Complexity = O(n^2)
  2. Space Complexity = O(1)

Implementation 3 : O(n^2) Optimization - Handling duplicates in beautiful way 😊

  1. class Solution {
  2. public List<List<Integer>> threeSum(int[] nums) {
  3. List<List<Integer>> response = new ArrayList<>();
  4. if(nums == null || nums.length < 3)
  5. return response;
  6. Arrays.sort(nums);
  7. int n = nums.length;
  8. for(int i = 0; i < n-2; i++) {
  9. if(i > 0 && nums[i] == nums[i-1])
  10. continue;
  11. int left = i+1;
  12. int right = n-1;
  13. while(left < right) {
  14. int sum = nums[i] + nums[left] + nums[right];
  15. if(sum == 0) {
  16. response.add(Arrays.asList(nums[i], nums[left], nums[right]));
  17. while(left < right && nums[left] == nums[left+1])
  18. left++;
  19. while(left < right && nums[right] == nums[right-1])
  20. right--;
  21. left++;
  22. right--;
  23. } else if(sum > 0) {
  24. right--;
  25. } else {
  26. left++;
  27. }
  28. }
  29. }
  30. return response;
  31. }
  32. }

Above implementation have Runtime complexity of O(n^2) and space complexity of O(1)

  1. Runtime Complexity = O(n^2)
  2. Space Complexity = O(1)

Note that, in above implementation, we are not using addResult() method to handle duplicate triplets, rather we are handling duplicate scenarios, when we are adding a triplet to the final result. This optimizes the runtime of the algorithm and makes it fast.

References :

  1. https://massivealgorithms.blogspot.com/2014/06/leetcode-3sum.html
  2. https://www.youtube.com/watch?v=qJSPYnS35SE