Guest User

Untitled

a guest
Oct 19th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.69 KB | None | 0 0
  1. class Solution {
  2. public List<List<Integer>> threeSum(int[] nums) {
  3. List<List<Integer>> result = new LinkedList<>();
  4. Arrays.sort(nums);
  5. for (int i = 0; i < nums.length - 2; i++) {
  6. twoSum(nums, i + 1, nums.length - 1, -nums[i], result);
  7. }
  8. return result;
  9. }
  10.  
  11. public void twoSum(int[] numbers, int i, int j, int target, List<List<Integer>> result) {
  12. while (i < j) {
  13. int sum = numbers[i] + numbers[j];
  14. if (sum == target) {
  15. result.add(Arrays.asList(-target, numbers[i], numbers[j]));
  16. i++; j--;
  17. }
  18. else if (sum > target) j--;
  19. else i++;
  20. }
  21. }
  22. }
Add Comment
Please, Sign In to add comment