Explore the 3Sum on the Leetcode:
Given an array S of n integers, is there elements a, 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]]
1. Brute Force solution
Time complexity up to O (n^3)
Public list<list<integer>> threesum (int[] nums) { list<list<integer>> lists =new Arraylist<list<integer>>(); for (int i=0;i<nums.length-2;i++) {for (int j = i + 1, J < Nums.length-1; J + +) {for (int z=j+1;z< nums.length;z++) { if (nums[i]+nums[j]+nums[z]==0) {list<integer> List =new Arraylist<integer > (); List.add (Nums[i]); List.add (Nums[j]); List.add (Nums[z]); Lists.add (list);}} }} return lists;}
Operation Result:
The result is unsatisfactory, there is a repetition. I thought about it if I used a list to row the weight. It is necessary to sort the list first, then compare whether it is equal, if it is equal, and then remove the same. The code is as follows:
public static list<list<integer>> three (int[] nums) { list<list<integer>> lists =new Arraylist<list<integer>>(); for (int i=0;i<nums.length-2;i++) {for (int j = i + 1, J < Nums.length-1; J + +) {for (int z=j+1;z< nums.length;z++) { if (nums[i]+nums[j]+nums[z]==0) {list<integer> List =new Arraylist<integer >();
Boolean flag = true; List.add (Nums[i]); List.add (Nums[j]); List.add (Nums[z]); Collections.sort (list); for (int k=0;i<lists.size (); i++) {if(list.equals (Lists.get (i))) {flag = false; }}
if (flag) {Lists.add (list);
}}}}} return lists;}
Operation Result:
Look at the seemingly problem solved, but runtime=2ms, time is a bit long, time complexity is too high.
When handing in a mistake:
Since the result can be run out, why also explode array out of bounds? I can't see what's wrong with it.
2. Use map
Time complexity O (n+n^2), O (n^2)
Public list<list<integer>> threesum (int[] nums) { list<list<integer>> lists =new Arraylist<list<integer>>(); map<integer,integer> map =new hashmap<integer,integer>(); for (int i=0;i<nums.length;i++) { map.put (nums[i],i); } for (int i=0;i<nums.length;i++) {for (int j=i+1;j<nums.length;j++) {int res=0-nums[i]-nums [j]; if (Map.containskey (res) &&map.get (res)!=i&&map.get (res)! =j) {list<integer> List =new Arraylist<integer>(); List.add (res); List.add (Nums[i]); List.add (Nums[j]); Lists.add (list);}} } return lists;}
The results of this operation make people headache.
There is no good way to arrange weight, if so:
Public list<list<integer>> threesum (int[] nums) { arrays.sort (nums); list<list<integer>> lists =new arraylist<list<integer>>(); map<integer,integer> map =new hashmap<integer,integer>(); for (int i=0;i<nums.length;i++) {for (int j=i+1;j<nums.length;j++) { int res=0-nums[i]- NUMS[J]; If(Map.containskey (res)) {list<integer> List =new arraylist<integer>(); List.add (res); List.add (Nums[i]); List.add (Nums[j]); Lists.add (list); }} map.put (Nums[i],i); } return lists;}
Operation Result:
This results in the correct, submit:
For the above array, the algorithm is not valid. I rewrote the list above, but I can't solve the problem. Please help me solve problems and learn together.
Attach the address of the Leetcode question:
3Sum
3Sum Discussion (Java)