Guest User

Untitled

a guest
Jul 22nd, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.75 KB | None | 0 0
  1. class Solution {
  2. public List<List<Integer>> threeSum(int[] nums) {
  3. Arrays.sort(nums);
  4. List<List<Integer>> L = new LinkedList<List<Integer>> ();
  5. Set<int[]> set = new HashSet<int[]> () ;
  6. int i = 0;
  7. int j , z;
  8. int sum = -1;
  9. while(i < nums.length-2){
  10. j = i + 1;
  11. z = nums.length-1;
  12. while(j < z){
  13. sum = nums[i] + nums[j] + nums[z];
  14. if(sum < 0)
  15. j++;
  16. else if(sum > 0)
  17. z--;
  18. else if (sum == 0){
  19. set.add( new int[] { nums[i], nums[j] , nums[z]} );
  20. break;
  21. }
  22.  
  23. }
  24. i++;
  25. }
  26. for( int[] each : set){
  27. L.add(Arrays.asList(each[0], each[1], each[2]));
  28. }
  29.  
  30. return L;
  31.  
  32. }
Add Comment
Please, Sign In to add comment