Skip to content

Commit 8422f84

Browse files
committed
add _3Sum - Day 8
1 parent e15b33b commit 8422f84

File tree

12 files changed

+133
-640
lines changed

12 files changed

+133
-640
lines changed

.idea/aws.xml

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/codeStyles/Project.xml

Lines changed: 4 additions & 48 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/codeStyles/codeStyleConfig.xml

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/dbnavigator.xml

Lines changed: 0 additions & 456 deletions
This file was deleted.

.idea/dictionaries/rajesh.xml

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/hydra.xml

Lines changed: 0 additions & 9 deletions
This file was deleted.

.idea/inspectionProfiles/Project_Default.xml

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/uiDesigner.xml

Lines changed: 0 additions & 124 deletions
This file was deleted.

.idea/sbt.xml renamed to .idea/vcs.xml

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="JAVA_MODULE" version="4">
3+
<component name="NewModuleRootManager" inherit-compiler-output="true">
4+
<exclude-output />
5+
<content url="file://$MODULE_DIR$">
6+
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
7+
</content>
8+
<orderEntry type="inheritedJdk" />
9+
<orderEntry type="sourceFolder" forTests="false" />
10+
<orderEntry type="library" name="scala-sdk-2.11.8" level="application" />
11+
</component>
12+
</module>
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package com.concept.scala.leetcode_30days_challenge_July2020
2+
3+
import scala.collection.mutable
4+
import scala.collection.mutable.ListBuffer
5+
6+
/** *
7+
* Day 8
8+
*
9+
* @todo 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.
10+
* @note The solution set must not contain duplicate triplets.
11+
* @example Given array nums = [-1, 0, 1, 2, -1, -4],
12+
*
13+
* A solution set is:
14+
* [
15+
* [-1, 0, 1],
16+
* [-1, -1, 2]
17+
* ]
18+
*
19+
*
20+
*
21+
*/
22+
object _3Sum {
23+
def main(args: Array[String]): Unit = {
24+
val nums = Array(-1, 0, 1, 2, -1, -4)
25+
println(threeSumByHashMap(nums))
26+
}
27+
28+
//MemoryLimitException
29+
def threeSum(nums: Array[Int]): List[List[Int]] =
30+
nums.sorted.combinations(3).map(x => if (x.sum == 0) x else 0).filter(x => x != 0).map(x => x.asInstanceOf[Array[Int]]).toArray.map(_.toList).toList
31+
32+
//RuntimeLimitException
33+
def threeSumByHashMap(nums: Array[Int]): List[List[Int]] = {
34+
import scala.collection.mutable
35+
val pairs = mutable.HashMap[Int, List[Int]]()
36+
nums.zipWithIndex.foreach({ case (x, idx) => pairs(x) = idx :: pairs.getOrElse(x, Nil) })
37+
38+
def twoSum(num1: Int, idx1: Int): List[List[Int]] = {
39+
val res = for {
40+
(num2, idx2) <- nums.iterator.zipWithIndex.collect({ case (x, idx) if idx != idx1 => x -> idx })
41+
num3 <- pairs.getOrElse(-num1 - num2, Nil).filterNot(List(idx1, idx2).contains(_)).map(nums).take(1)
42+
} yield {
43+
(num1 :: num2 :: num3 :: Nil).sorted
44+
}
45+
res.toList
46+
}
47+
48+
pairs.flatMap({ case (key, value :: tail) => twoSum(key, value) case _ => Nil })
49+
.foldLeft(List[List[Int]]())({ case (acc, x) if !acc.contains(x) => x :: acc case (acc, _) => acc })
50+
51+
52+
}
53+
54+
55+
}
56+
57+
/*
58+
59+
Java alternative
60+
class Solution {
61+
public List<List<Integer>> threeSum(int[] nums) {
62+
List<List<Integer>> result = new ArrayList<>();
63+
Arrays.sort(nums);
64+
for (int i = 0; i + 2 < nums.length; i++) {
65+
if (nums[i] > 0) break;
66+
if (i > 0 && nums[i] == nums[i - 1]) {
67+
continue;
68+
}
69+
int j = i + 1, k = nums.length - 1;
70+
int target = -nums[i];
71+
while (j < k) {
72+
if (nums[j] + nums[k] == target) {
73+
result.add(Arrays.asList(nums[i], nums[j], nums[k]));
74+
j++;
75+
k--;
76+
while (j < k && nums[j] == nums[j - 1]) j++;
77+
while (j < k && nums[k] == nums[k + 1]) k--;
78+
} else if (nums[j] + nums[k] > target) {
79+
k--;
80+
} else {
81+
j++;
82+
}
83+
}
84+
}
85+
return result;
86+
87+
}
88+
}
89+
*/

0 commit comments

Comments
 (0)