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.
Example 1 2 3 4 5 6 7 8 9 Example: Given array nums = [-1, 0, 1, 2, -1, -4], A solution set is: [ [-1, 0, 1], [-1, -1, 2] ]
JAVA题解 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 package algorithm;import java.util.*;public class Leetcode15 { public static List<List<Integer>> threeSum(int [] nums) { List<List<Integer>> res = new ArrayList<>(); Set<String> ids = new HashSet<>(); for (int i = 0 ; i < nums.length - 2 ; i++) { for (int j = i+1 ; j < nums.length-1 ; j++) { for (int k = j+1 ; k < nums.length; k++) { if (nums[i]+nums[j]+nums[k] == 0 ){ String i2 = nums[i] + "" +nums[j]+nums[k]; System.out.println(nums[i]+"," +nums[j]+"," +nums[k]+"=" +i2); if (!ids.contains(i2)){ ids.add(i2); res.add(Arrays.asList(nums[i],nums[j],nums[k])); } } } } } return res; } public static List<List<Integer>> threeSum1(int [] nums) { List<List<Integer>> ans = new ArrayList(); int len = nums.length; if (nums == null || len < 3 ) return ans; Arrays.sort(nums); for (int i = 0 ; i < len; i++) { if (nums[i] > 0 ) break ; if (i > 0 && nums[i] == nums[i - 1 ]) continue ; int L = i + 1 ; int R = len - 1 ; while (L < R) { int sum = nums[i] + nums[L] + nums[R]; if (sum == 0 ) { ans.add(Arrays.asList(nums[i], nums[L], nums[R])); while (L < R && nums[L] == nums[L + 1 ]) L++; while (L < R && nums[R] == nums[R - 1 ]) R--; L++; R--; } else if (sum < 0 ) L++; else if (sum > 0 ) R--; } } return ans; } public static void main (String[] args) { System.out.println(threeSum(new int []{-4 , -2 , -2 , -2 , 0 , 1 , 2 , 2 , 2 , 3 , 3 , 4 , 4 , 6 , 6 })); } }
Given an array nums of n integers and an integer target, find three integers in nums such that the sum is closest to target. Return the sum of the three integers. You may assume that each input would have exactly one solution.
Example 1 2 3 4 5 6 Example: Given array nums = [-1, 2, 1, -4], and target = 1. The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).
JAVA题解 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 package algorithm;import java.util.Arrays;public class Leetcode16 { public int threeSumClosest (int [] nums, int target) { return 0 ; } public int threeSumClosest1 (int [] nums, int target) { Arrays.sort(nums); int ans = nums[0 ] + nums[1 ] + nums[2 ]; for (int i=0 ;i<nums.length;i++) { int start = i+1 , end = nums.length - 1 ; while (start < end) { int sum = nums[start] + nums[end] + nums[i]; if (Math.abs(target - sum) < Math.abs(target - ans)) ans = sum; if (sum > target) end--; else if (sum < target) start++; else return ans; } } return ans; } }