From 538166bea877349c255b568dd58c847d8d2ce3c2 Mon Sep 17 00:00:00 2001 From: HoaPhan Date: Mon, 18 Mar 2024 05:27:10 +1100 Subject: [PATCH] https://leetcode.com/problems/3sum/description/ --- .../java/ide_handicapped/3sum/Solution.java | 102 ++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 interview_prep/algorithm/java/ide_handicapped/3sum/Solution.java diff --git a/interview_prep/algorithm/java/ide_handicapped/3sum/Solution.java b/interview_prep/algorithm/java/ide_handicapped/3sum/Solution.java new file mode 100644 index 0000000..04b666b --- /dev/null +++ b/interview_prep/algorithm/java/ide_handicapped/3sum/Solution.java @@ -0,0 +1,102 @@ +import java.util.AbstractList; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +class Solution { + private List> res; + + public List> threeSum(int[] nums) { + + return new AbstractList>() { + public List get(int index) { + init(); + return res.get(index); + } + + public int size() { + init(); + return res.size(); + } + + private void init() { + if (res != null) + return; + Arrays.sort(nums); + int l, r, sum; + final Set> tempRes = new HashSet<>(); + for (int i = 0; i < nums.length - 2; ++i) { + l = i + 1; + r = nums.length - 1; + while (l < r) { + sum = nums[i] + nums[l] + nums[r]; + if (sum == 0) + tempRes.add(Arrays.asList(nums[i], nums[l], nums[r])); + if (sum < 0) + ++l; + else + --r; + } + } + res = new ArrayList>(tempRes); + } + + }; + } + + public static void main(String[] args) { + // https://leetcode.com/problems/3sum/description/ + t1(); + t2(); + t3(); + + } + + public static void t1() { + final Solution sol = new Solution(); + final List res = sol.threeSum(new int[] { -1, 0, 1, 2, -1, -4 }); + assert res.contains(Stream.of(-1, -1, 2).collect(Collectors.toList())); + assert res.contains(Stream.of(-1, 0, 1).collect(Collectors.toList())); + // System.out.println(res); + } + + public static void t2() { + final Solution sol = new Solution(); + final List res = sol.threeSum(new int[] { 0, 1, 1 }); + assert res.isEmpty(); + } + + public static void t3() { + final Solution sol = new Solution(); + final List res = sol.threeSum(new int[] { 0, 0, 0 }); + assert res.contains(Stream.of(0, 0, 0).collect(Collectors.toList())); + // System.out.println(res); + } + + public List> threeSumSlow(int[] nums) { + if (nums == null || nums.length == 0) + return Collections.emptyList(); + Arrays.sort(nums); + int l, r, sum; + final Set> tempRes = new HashSet<>(); + for (int i = 0; i < nums.length - 2; ++i) { + l = i + 1; + r = nums.length - 1; + while (l < r) { + sum = nums[i] + nums[l] + nums[r]; + if (sum == 0) + tempRes.add(Arrays.asList(nums[i], nums[l], nums[r])); + if (sum < 0) + ++l; + else + --r; + } + } + return new ArrayList>(tempRes); + } +} \ No newline at end of file