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 36699ae

Browse files
905: Sort Array By Parity
1 parent b515ed4 commit 36699ae

File tree

4 files changed

+73
-0
lines changed

4 files changed

+73
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ My solutions to LeetCode problems in Kotlin.
3131
| [773](https://leetcode.com/problems/sliding-puzzle/) | [Sliding Puzzle](src/main/kotlin/com/schmoczer/leetcode/_0773/SlidingPuzzle.kt) | Hard |
3232
| [796](https://leetcode.com/problems/rotate-string/) | [Rotate String](src/main/kotlin/com/schmoczer/leetcode/_0796/RotateString.kt) | Easy |
3333
| [876](https://leetcode.com/problems/middle-of-the-linked-list/) | [Middle of the Linked List](src/main/kotlin/com/schmoczer/leetcode/_0876/MiddleOfTheLinkedList.kt) | Easy |
34+
| [905](https://leetcode.com/problems/sort-array-by-parity/) | [Sort Array By Parity](src/main/kotlin/com/schmoczer/leetcode/_0905/SortArrayByParity.kt) | Easy |
3435
| [941](https://leetcode.com/problems/valid-mountain-array/) | [Valid Mountain Array](src/main/kotlin/com/schmoczer/leetcode/_0941/ValidMountainArray.kt) | Easy |
3536
| [977](https://leetcode.com/problems/squares-of-a-sorted-array/) | [Squares of a Sorted Array](src/main/kotlin/com/schmoczer/leetcode/_0977/SquaresOfASortedArray.kt) | Easy |
3637
| [1089](https://leetcode.com/problems/duplicate-zeros/) | [Duplicate Zeros](src/main/kotlin/com/schmoczer/leetcode/_1089/DuplicateZeros.kt) | Easy |
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Sort Array By Parity
2+
3+
Given an integer array `nums`, move all the even integers at the beginning of the array followed by all the odd
4+
integers.
5+
6+
Return any array that satisfies this condition.
7+
8+
Example 1:
9+
10+
> Input: nums = [3,1,2,4]
11+
>
12+
> Output: [2,4,3,1]
13+
14+
Example 2:
15+
16+
> Input: nums = [0]
17+
>
18+
> Output: [0]
19+
20+
Constraints:
21+
22+
- `1 <= nums.length <= 5000`
23+
- `0 <= nums[i] <= 5000`
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.schmoczer.leetcode._0905
2+
3+
class SortArrayByParity {
4+
// Runtime 1ms Beats 96.34%
5+
fun sortArrayByParity(nums: IntArray): IntArray {
6+
var slow = 0
7+
for (fast in 0 until nums.size) {
8+
if (nums[fast] and 1 == 0) {
9+
val number = nums[slow]
10+
nums[slow] = nums[fast]
11+
nums[fast] = number
12+
slow++
13+
}
14+
}
15+
return nums
16+
}
17+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.schmoczer.leetcode._0905
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.assertContentEquals
8+
9+
class SortArrayByParityTest {
10+
private companion object {
11+
@JvmStatic
12+
fun unsortedAndSorted() = listOf(
13+
Arguments.of(intArrayOf(3, 1, 2, 4), intArrayOf(2, 4, 3, 1)),
14+
Arguments.of(intArrayOf(0), intArrayOf(0)),
15+
)
16+
}
17+
18+
private lateinit var sut: SortArrayByParity
19+
20+
@BeforeEach
21+
fun setUp() {
22+
sut = SortArrayByParity()
23+
}
24+
25+
@ParameterizedTest(name = "{0} sorted by parity is {1}")
26+
@MethodSource("unsortedAndSorted")
27+
fun `returns the array sorted by parity`(input: IntArray, expected: IntArray) {
28+
val result = sut.sortArrayByParity(input)
29+
30+
assertContentEquals(expected, result)
31+
}
32+
}

0 commit comments

Comments
(0)

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