Content-Length: 338970 | pFad | http://github.com/hajin-kim/algorithm-leetcode/commit/#start-of-content

68321B75 Time: 154 ms (12.31%), Space: 55.3 MB (11.28%) - LeetHub · hajin-kim/algorithm-leetcode@d490e9e · GitHub
Skip to content

Commit d490e9e

Browse files
committed
Time: 154 ms (12.31%), Space: 55.3 MB (11.28%) - LeetHub
1 parent dc5301a commit d490e9e

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

0018-4sum/0018-4sum.kt

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
class Solution {
2+
fun fourSum(nums: IntArray, target: Int): List<List<Int>> {
3+
val target = target.toLong()
4+
5+
nums.sort()
6+
7+
val result = mutableListOf<List<Int>>()
8+
9+
var prevA: Int? = null
10+
var prevB: Int? = null
11+
var prevC: Int? = null
12+
13+
for (a in nums.indices) {
14+
if (nums[a] == prevA) {
15+
continue
16+
}
17+
18+
for (b in (a + 1)..nums.lastIndex) {
19+
if (nums[a] == prevA && nums[b] == prevB) {
20+
continue
21+
}
22+
23+
for (c in (b + 1)..nums.lastIndex) {
24+
if (nums[a] == prevA && nums[b] == prevB && nums[c] == prevC) {
25+
continue
26+
}
27+
28+
val sum3 = nums[a].toLong() + nums[b] + nums[c]
29+
for (d in (c + 1)..nums.lastIndex) {
30+
val sum4 = sum3 + nums[d]
31+
if (sum4 > target) {
32+
break
33+
} else if (sum4 == target) {
34+
result += listOf(nums[a], nums[b], nums[c], nums[d])
35+
prevA = nums[a]
36+
prevB = nums[b]
37+
prevC = nums[c]
38+
break
39+
}
40+
}
41+
}
42+
}
43+
}
44+
45+
return result
46+
}
47+
}

0 commit comments

Comments
 (0)








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/hajin-kim/algorithm-leetcode/commit/#start-of-content

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy