Skip to content

Commit b45c728

Browse files
committed
exercise/id15
1 parent 7e715e2 commit b45c728

File tree

4 files changed

+133
-0
lines changed

4 files changed

+133
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ My Kotlin exercises with tests from [Leetcode](https://leetcode.com/kotlerdev)
5555
| 217 | [Contains Duplicate](src/main/kotlin/exercise/easy/id217/Description217.md) | [solution](src/main/kotlin/exercise/easy/id217/Solution217.kt) | [test](src/test/kotlin/exercise/easy/id217/Solution217Test.kt) | Easy |
5656
| 283 | [Move Zeroes](src/main/kotlin/exercise/easy/id283/Description283.md) | [solution](src/main/kotlin/exercise/easy/id283/Solution283.kt) | [test](src/test/kotlin/exercise/easy/id283/Solution283Test.kt) | Easy |
5757
| 977 | [Squares of a Sorted Array](src/main/kotlin/exercise/easy/id977/Description977.md) | [solution](src/main/kotlin/exercise/easy/id977/Solution977.kt) | [test](src/test/kotlin/exercise/easy/id977/Solution977Test.kt) | Easy |
58+
| 15 | [3Sum](src/main/kotlin/exercise/medium/id15/Description15.md) | [solution](src/main/kotlin/exercise/medium/id15/Solution15.kt) | [test](src/test/kotlin/exercise/medium/id15/Solution15Test.kt) | Medium |
5859
| 57 | [Insert Internal](src/main/kotlin/exercise/medium/id57/Description57.md) | [solution](src/main/kotlin/exercise/medium/id57/Solution57.kt) | [test](src/test/kotlin/exercise/medium/id57/Solution57Test.kt) | Medium |
5960

6061
#### String
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
[//]: # (Copyright [2023] [Anton Kotler kotler.developer@gmail.com] License MIT)
2+
3+
# 3Sum
4+
5+
![Difficulty](https://img.shields.io/badge/Difficulty-Medium-548af7)
6+
7+
Given an integer array nums, return all the triples `[nums[i], nums[j]], nums[k]]`, such that `i != j`, `i != k`,
8+
and `j != k`, and `nums[i] + nums[j] + nums[k] == 0`.
9+
Notice that the solution set must not contain duplicate triplets.
10+
11+
Example 1:
12+
13+
```
14+
Input: nums = [-1,0,1,2,-1,-4]
15+
Output: [[-1,-1,2],[-1,0,1]]
16+
Explanation:
17+
nums[0] + nums[1] + nums[2] = (-1) + 0 + 1 = 0.
18+
nums[1] + nums[2] + nums[4] = 0 + 1 + (-1) = 0.
19+
nums[0] + nums[3] + nums[4] = (-1) + 2 + (-1) = 0.
20+
The distinct triplets are [-1,0,1] and [-1,-1,2].
21+
Notice that the order of the output and the order of the triplets does not matter.
22+
```
23+
24+
Example 2:
25+
26+
```
27+
Input: nums = [0,1,1]
28+
Output: []
29+
Explanation: The only possible triplet does not sum up to 0.
30+
```
31+
32+
Example 3:
33+
34+
```
35+
Input: nums = [0,0,0]
36+
Output: [[0,0,0]]
37+
Explanation: The only possible triplet sums up to 0.
38+
```
39+
40+
Constraints:
41+
42+
- `3 <= nums.length <= 3000`
43+
- `-10^5 <= nums[i] <= 10^5`
44+
45+
| ID | Description | Solution | Test | Difficulty |
46+
|:--:|:------------|:---------------------------:|:--------------------------------------------------------------------------------:|:----------:|
47+
| 15 | 3Sum | [solution](./Solution15.kt) | [test](../../../../../../src/test/kotlin/exercise/medium/id15/Solution15Test.kt) | Medium |
48+
49+
:top: [Back to all topics](https://github.com/kotler-dev/kotlin-leetcode)
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package exercise.medium.id15
2+
3+
4+
class Solution15 {
5+
fun threeSum(nums: IntArray): List<List<Int>> {
6+
if (nums.isEmpty() || nums.size < 3) return emptyList()
7+
8+
val numList = mutableListOf<List<Int>>()
9+
var currentIndex = 0
10+
11+
12+
while (currentIndex < nums.size) {
13+
var shiftIndex = 0
14+
val list = mutableListOf<Int>(0, 0, 0)
15+
list[0] = nums[currentIndex]
16+
var step = 1
17+
18+
while (shiftIndex < nums.size) {
19+
if (shiftIndex != currentIndex) {
20+
list[step] = nums[shiftIndex]
21+
step++
22+
if (step == 3) {
23+
println("current: ${currentIndex}, list3: ${list}")
24+
// if ((list[0] != list[1]) && (list[0] != list[2]) && (list[1] != list[2])) {
25+
if ((list[0] + list[1] + list[2] == 0)) {
26+
list.sort()
27+
if (list !in numList) numList.add(list.toList())
28+
}
29+
step = 1
30+
shiftIndex--
31+
}
32+
}
33+
shiftIndex++
34+
}
35+
currentIndex++
36+
}
37+
println(numList)
38+
39+
return numList
40+
}
41+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package exercise.medium.id15
2+
3+
import io.kotest.assertions.print.print
4+
import io.kotest.core.spec.style.FunSpec
5+
import io.kotest.datatest.withData
6+
import io.kotest.matchers.collections.shouldContainAll
7+
import io.kotest.matchers.equals.shouldBeEqual
8+
import io.kotest.matchers.shouldBe
9+
10+
data class TestCase(val nums: IntArray, val expected: List<List<Int>>)
11+
12+
class Solution15Test : FunSpec({
13+
14+
context("shouldBe") {
15+
withData(
16+
nameFn = { "${it.nums.print().value}, expected: ${it.expected.print().value}" },
17+
TestCase(
18+
nums = intArrayOf(-1, 0, 1, 2, -1, -4, -2, -3, 3, 0, 4), expected = listOf(
19+
listOf(-4, 0, 4),
20+
listOf(-4, 1, 3),
21+
listOf(-3, -1, 4),
22+
listOf(-3, 0, 3),
23+
listOf(-3, 1, 2),
24+
listOf(-2, -1, 3),
25+
listOf(-2, 0, 2),
26+
listOf(-1, -1, 2),
27+
listOf(-1, 0, 1),
28+
)
29+
),
30+
TestCase(
31+
nums = intArrayOf(3, 0, -2, -1, 1, 2),
32+
expected = listOf(listOf(-2, -1, 3), listOf(-2, 0, 2), listOf(-1, 0, 1))
33+
),
34+
TestCase(nums = intArrayOf(-1, 0, 1, 2, -1, -4), expected = listOf(listOf(-1, -1, 2), listOf(-1, 0, 1))),
35+
TestCase(nums = intArrayOf(0, 0, 0), expected = listOf(listOf(0, 0, 0))),
36+
TestCase(nums = intArrayOf(0, 0, 0, 0), expected = listOf(listOf(0, 0, 0))),
37+
TestCase(nums = intArrayOf(0, 1, 1), expected = emptyList()),
38+
) { (nums, expected) ->
39+
Solution15().threeSum(nums) shouldContainAll expected
40+
}
41+
}
42+
})

0 commit comments

Comments
 (0)