Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit afbab2f

Browse files
27: Remove Element
1 parent 6704573 commit afbab2f

File tree

4 files changed

+91
-1
lines changed

4 files changed

+91
-1
lines changed

‎README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ My solutions to LeetCode problems in Kotlin.
1010
| [5](https://leetcode.com/problems/longest-palindromic-substring/) | [Longest Palindromic Substring](src/main/kotlin/com/schmoczer/leetcode/_0005/LongestPalindromicSubstring.kt) | Medium |
1111
| [8](https://leetcode.com/problems/string-to-integer-atoi/) | [String to Integer (atoi)](src/main/kotlin/com/schmoczer/leetcode/_0008/StringToInteger.kt) | Medium |
1212
| [20](https://leetcode.com/problems/valid-parentheses/) | [Valid Parentheses](src/main/kotlin/com/schmoczer/leetcode/_0020/ValidParentheses.kt) | Easy |
13-
| [42](https://leetcode.com/problems/trapping-rain-water/) | [Trapping Rain Water](src/main/kotlin/com/schmoczer/leetcode/_0042/TrappingRainWater.kt) | Hard |
13+
| [27](https://leetcode.com/problems/remove-element/) | [Remove Element](src/main/kotlin/com/schmoczer/leetcode/_0027/README.md) | Easy |
14+
[42](https://leetcode.com/problems/trapping-rain-water/) | [Trapping Rain Water](src/main/kotlin/com/schmoczer/leetcode/_0042/TrappingRainWater.kt) | Hard |
1415
| [48](https://leetcode.com/problems/rotate-image/) | [Rotate Image](src/main/kotlin/com/schmoczer/leetcode/_0048/RotateImage.kt) | Medium |
1516
| [49](https://leetcode.com/problems/group-anagrams/) | [Group Anagrams](src/main/kotlin/com/schmoczer/leetcode/_0049/GroupAnagrams.kt) | Medium |
1617
| [54](https://leetcode.com/problems/spiral-matrix/) | [Spiral Matrix](src/main/kotlin/com/schmoczer/leetcode/_0054/SpiralMatrix.kt) | Medium |
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Remove Element
2+
3+
Given an integer array `nums` and an integer `val`, remove all occurrences of `val` in `nums` in-place. The order of the
4+
elements may be changed. Then return the number of elements in `nums` which are not equal to `val`.
5+
6+
Consider the number of elements in `nums` which are not equal to `val` be `k`, to get accepted, you need to do the
7+
following things:
8+
9+
- Change the array `nums` such that the first `k` elements of `nums` contain the elements which are not equal to `val`.
10+
The remaining elements of `nums` are not important as well as the size of `nums`.
11+
- Return `k`.
12+
13+
Example 1:
14+
15+
> Input: nums = [3,2,2,3], val = 3
16+
>
17+
> Output: 2, nums = [2,2,_,_]
18+
19+
Example 2:
20+
21+
> Input: nums = [0,1,2,2,3,0,4,2], val = 2
22+
>
23+
> Output: 5, nums = [0,1,4,0,3,_,_,_]
24+
25+
Constraints:
26+
27+
- `0 <= nums.length <= 100`
28+
- `0 <= nums[i] <= 50`
29+
- `0 <= val <= 100`
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.schmoczer.leetcode._0027
2+
3+
class RemoveElement {
4+
// Runtime 0ms Beats 100.00%
5+
fun removeElement(nums: IntArray, `val`: Int): Int {
6+
val copied = IntArray(nums.size)
7+
System.arraycopy(nums, 0, copied, 0, nums.size)
8+
var result = 0
9+
var index = 0
10+
for (number in copied) {
11+
if (number == `val`) {
12+
result++
13+
} else {
14+
nums[index] = number
15+
index++
16+
}
17+
}
18+
return index
19+
}
20+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.schmoczer.leetcode._0027
2+
3+
import org.junit.jupiter.api.BeforeEach
4+
import org.junit.jupiter.params.ParameterizedTest
5+
import org.junit.jupiter.params.provider.Arguments
6+
import org.junit.jupiter.params.provider.MethodSource
7+
import kotlin.test.assertEquals
8+
9+
class RemoveElementTest {
10+
private companion object {
11+
@JvmStatic
12+
fun numsAndVals() = listOf(
13+
Arguments.of(intArrayOf(3, 2, 2, 3), 3, 2, intArrayOf(2, 2)),
14+
Arguments.of(intArrayOf(0, 1, 2, 2, 3, 0, 4, 2), 2, 5, intArrayOf(0, 1, 3, 0, 4)),
15+
)
16+
}
17+
18+
private lateinit var sut: RemoveElement
19+
20+
@BeforeEach
21+
fun setUp() {
22+
sut = RemoveElement()
23+
}
24+
25+
@ParameterizedTest(name = "There are {2} elements left after removing {1} from {0}")
26+
@MethodSource("numsAndVals")
27+
fun `returns the number of occurences and removes the number`(
28+
nums: IntArray,
29+
`val`: Int,
30+
expectedLength: Int,
31+
expectedNums: IntArray,
32+
) {
33+
val result = sut.removeElement(nums, `val`)
34+
35+
assertEquals(expectedLength, result)
36+
for (i in 0 until expectedLength) {
37+
assertEquals(expectedNums[i], nums[i])
38+
}
39+
}
40+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /