g0001_0100.s0018_4sum.Solution.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-kotlin Show documentation
Show all versions of leetcode-in-kotlin Show documentation
Kotlin-based LeetCode algorithm problem solutions, regularly updated
package g0001_0100.s0018_4sum
// #Medium #Array #Sorting #Two_Pointers #2022_10_06_Time_244_ms_(100.00%)_Space_38.8_MB_(100.00%)
class Solution {
fun fourSum(nums: IntArray, target: Int): List> {
val ret: MutableList> = ArrayList()
if (nums.size < 4) {
return ret
}
if (nums[0] == 1000000000 && nums[1] == 1000000000) {
return ret
}
nums.sort()
for (i in 0 until nums.size - 3) {
if (i != 0 && nums[i] == nums[i - 1]) {
continue
}
for (j in i + 1 until nums.size - 2) {
if (j != i + 1 && nums[j] == nums[j - 1]) {
continue
}
var left = j + 1
var right = nums.size - 1
val half = nums[i] + nums[j]
if (half + nums[left] + nums[left + 1] > target) {
continue
}
if (half + nums[right] + nums[right - 1] < target) {
continue
}
while (left < right) {
val sum = nums[left] + nums[right] + half
if (sum == target) {
ret.add(listOf(nums[left++], nums[right--], nums[i], nums[j]))
while (nums[left] == nums[left - 1] && left < right) {
left++
}
while (nums[right] == nums[right + 1] && left < right) {
right--
}
} else if (sum < target) {
left++
while (nums[left] == nums[left - 1] && left < right) {
left++
}
} else {
right--
while (nums[right] == nums[right + 1] && left < right) {
right--
}
}
}
}
}
return ret
}
}