-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
0018-4sum.kt
46 lines (40 loc) · 1.42 KB
/
0018-4sum.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
class Solution {
fun fourSum(nums: IntArray, target: Int): List<List<Int>> {
nums.sort()
val res = ArrayList<ArrayList<Int>>()
val temp = ArrayList<Int>()
fun kSum(k: Int, start: Int, targetSum: Long) {
if (k != 2) {
for (i in start..(nums.size - k)) {
if (i > start && nums[i - 1] == nums[i])
continue
temp.add(0, nums[i])
kSum(k - 1, i + 1, targetSum - nums[i])
temp.removeAt(0)
}
return
}
var left = start
var right = nums.lastIndex
while (left < right) {
val sum = nums[left].toLong() + nums[right].toLong()
if (sum < targetSum) {
left++
} else if (sum > targetSum) {
right--
} else {
temp.add(0, nums[left])
temp.add(0, nums[right])
res.add(ArrayList(temp))
temp.removeAt(0)
temp.removeAt(0)
left++
while (left < right && nums[left - 1] == nums[left])
left++
}
}
}
kSum(4, 0, target.toLong())
return res
}
}