-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1089-DuplicateZeros.go
More file actions
111 lines (99 loc) · 2.8 KB
/
1089-DuplicateZeros.go
File metadata and controls
111 lines (99 loc) · 2.8 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package main
// 1089. Duplicate Zeros
// Given a fixed-length integer array arr, duplicate each occurrence of zero, shifting the remaining elements to the right.
// Note that elements beyond the length of the original array are not written.
// Do the above modifications to the input array in place and do not return anything.
// Example 1:
// Input: arr = [1,0,2,3,0,4,5,0]
// Output: [1,0,0,2,3,0,0,4]
// Explanation: After calling your function, the input array is modified to: [1,0,0,2,3,0,0,4]
// Example 2:
// Input: arr = [1,2,3]
// Output: [1,2,3]
// Explanation: After calling your function, the input array is modified to: [1,2,3]
// Constraints:
// 1 <= arr.length <= 10^4
// 0 <= arr[i] <= 9
import "fmt"
func duplicateZeros(arr []int) {
i, j, n := 0, 0, len(arr)
back := make([]int, n)
copy(back, arr)
for j < n {
if back[i] == 0 { // 遇 0 则复写
arr[j] = 0
j++
if j >= n { break }
arr[j] = 0
} else {
arr[j] = back[i]
}
j++
i++
}
// return res
}
func duplicateZeros1(arr []int) {
n, span, end := len(arr), 0, 0
for i, v := range arr {
if v == 0 {
n -= 2
} else {
n -= 1
}
if n <= 0 {
end = i
span = len(arr) - 1 - i
break
}
}
if n == -1 {
arr[len(arr)-1] = 0
end--
}
for span > 0 {
v := arr[end]
if v != 0 {
arr[end+span] = v
} else {
arr[end+span] = v
span--
arr[end+span] = 0
}
end--
}
}
func main() {
// Example 1:
// Input: arr = [1,0,2,3,0,4,5,0]
// Output: [1,0,0,2,3,0,0,4]
// Explanation: After calling your function, the input array is modified to: [1,0,0,2,3,0,0,4]
arr1 := []int{1,0,2,3,0,4,5,0}
fmt.Println(arr1) // [1,0,2,3,0,4,5,0]
duplicateZeros(arr1)
fmt.Println(arr1) // [1,0,0,2,3,0,0,4]
// Example 2:
// Input: arr = [1,2,3]
// Output: [1,2,3]
// Explanation: After calling your function, the input array is modified to: [1,2,3]
arr2 := []int{1,2,3}
fmt.Println(arr2) // [1,2,3]
duplicateZeros(arr2)
fmt.Println(arr2) // [1,2,3]
arr3 := []int{0,0,0,0,0,0,0}
fmt.Println(arr3) // [0,0,0,0,0,0,0]
duplicateZeros(arr3)
fmt.Println(arr3) // [0,0,0,0,0,0,0]
arr11 := []int{1,0,2,3,0,4,5,0}
fmt.Println(arr11) // [1,0,2,3,0,4,5,0]
duplicateZeros1(arr11)
fmt.Println(arr11) // [1,0,0,2,3,0,0,4]
arr12 := []int{1,2,3}
fmt.Println(arr12) // [1,2,3]
duplicateZeros1(arr12)
fmt.Println(arr12) // [1,2,3]
arr13 := []int{0,0,0,0,0,0,0}
fmt.Println(arr13) // [0,0,0,0,0,0,0]
duplicateZeros1(arr13)
fmt.Println(arr13) // [0,0,0,0,0,0,0]
}