-
Notifications
You must be signed in to change notification settings - Fork 8
/
Solution.kt
60 lines (55 loc) · 1.33 KB
/
Solution.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/**
* Created by Inno Fang on 2018/1/4.
*/
/**
* 21 / 21 test cases passed.
* Status: Accepted
* Runtime: 542 ms
*/
class Solution {
fun moveZeroes(nums: IntArray): Unit {
var zeroes = 0
((nums.size - 1) downTo 0).forEach { i ->
if (nums[i] == 0) {
zeroes++
(i..(nums.size - 1 - zeroes)).forEach { j ->
nums[j] = nums[j + 1]
}
}
}
((nums.size - zeroes)..(nums.size - 1)).forEach { nums[it] = 0 }
}
}
/**
* 21 / 21 test cases passed.
* Status: Accepted
* Runtime: 492 ms
*/
class Solution2 {
fun moveZeroes(nums: IntArray): Unit {
var zeroes = 0
nums.indices.forEach {
when (nums[it]) {
0 -> zeroes++
else -> {
if (zeroes != 0) {
nums[it - zeroes] = nums[it]
nums[it] = 0
}
}
}
}
}
}
class Solution3 {
fun moveZeroes(nums: IntArray): Unit {
var zeroes = 0
nums.forEach { if (it != 0) nums[zeroes++] = it }
(zeroes until nums.size).forEach { nums[it] = 0 }
}
}
fun main(args: Array<String>) {
val nums = intArrayOf(0, 1, 0, 3, 12)
Solution3().moveZeroes(nums)
nums.forEach(::println)
}