Skip to content
This repository was archived by the owner on Sep 20, 2023. It is now read-only.

Commit 0298393

Browse files
committed
1089 done
1 parent 5112689 commit 0298393

File tree

2 files changed

+24
-11
lines changed

2 files changed

+24
-11
lines changed

Algorithms/1089.duplicate-zeros/duplicate-zeros.go

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,27 @@ package problem1089
33
func duplicateZeros(A []int) {
44
n := len(A)
55
//
6-
i, count := 0, 0
7-
for i+count < n {
6+
count := 0
7+
for i := 0; i < n; i++ {
88
if A[i] == 0 {
99
count++
1010
}
11-
i++
1211
}
13-
i--
12+
// copy A[i] to A[j]
13+
copy := func(i, j int) {
14+
if j < n {
15+
A[j] = A[i]
16+
}
17+
}
1418
//
15-
j := n - 1
16-
for count > 0 {
19+
i, j := n-1, n+count-1
20+
for i < j {
21+
copy(i, j)
1722
if A[i] == 0 {
18-
A[j-1], A[j] = 0, 0
19-
j -= 2
20-
count--
21-
} else {
22-
A[j] = A[i]
2323
j--
24+
copy(i, j)
2425
}
2526
i--
27+
j--
2628
}
2729
}

Algorithms/1089.duplicate-zeros/duplicate-zeros_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,17 @@ var tcs = []struct {
1212
ans []int
1313
}{
1414

15+
{
16+
17+
[]int{8, 4, 5, 0, 0, 0, 0, 7},
18+
[]int{8, 4, 5, 0, 0, 0, 0, 0},
19+
},
20+
21+
{
22+
[]int{0, 0, 0, 0, 0, 0, 0, 0},
23+
[]int{0, 0, 0, 0, 0, 0, 0, 0},
24+
},
25+
1526
{
1627
[]int{0, 0, 0, 0, 0, 0, 0},
1728
[]int{0, 0, 0, 0, 0, 0, 0},

0 commit comments

Comments
 (0)