Skip to content

Commit c416a5d

Browse files
committed
Add solution and test-cases for problem 1089
1 parent d569e0c commit c416a5d

File tree

3 files changed

+51
-16
lines changed

3 files changed

+51
-16
lines changed

leetcode/1001-1100/1089.Duplicate-Zeros/README.md

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,25 @@
11
# [1089.Duplicate Zeros][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
Given a fixed-length integer array `arr`, duplicate each occurrence of zero, shifting the remaining elements to the right.
5+
6+
**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.
77

88
**Example 1:**
99

1010
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
11+
Input: arr = [1,0,2,3,0,4,5,0]
12+
Output: [1,0,0,2,3,0,0,4]
13+
Explanation: After calling your function, the input array is modified to: [1,0,0,2,3,0,0,4]
1314
```
1415

15-
## 题意
16-
> ...
17-
18-
## 题解
16+
**EXample 2:**
1917

20-
### 思路1
21-
> ...
22-
Duplicate Zeros
23-
```go
2418
```
25-
19+
Input: arr = [1,2,3]
20+
Output: [1,2,3]
21+
Explanation: After calling your function, the input array is modified to: [1,2,3]
22+
```
2623

2724
## 结语
2825

leetcode/1001-1100/1089.Duplicate-Zeros/Solution.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,17 @@ func Solution(arr []int) {
1313
}
1414
}
1515
}
16+
17+
func Solution1(arr []int) {
18+
tmp := make([]int, len(arr))
19+
index, i := 0, 0
20+
for ; i < len(arr) && index < len(arr); i++ {
21+
if arr[i] != 0 {
22+
tmp[index] = arr[i]
23+
index++
24+
continue
25+
}
26+
index += 2
27+
}
28+
copy(arr, tmp)
29+
}

leetcode/1001-1100/1089.Duplicate-Zeros/Solution_test.go

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,34 @@ func TestSolution(t *testing.T) {
3030
}
3131
}
3232

33-
// 压力测试
33+
func TestSolution1(t *testing.T) {
34+
// 测试用例
35+
cases := []struct {
36+
name string
37+
inputs []int
38+
expect []int
39+
}{
40+
{"TestCase", []int{1, 0, 2, 3, 0, 4, 5, 0}, []int{1, 0, 0, 2, 3, 0, 0, 4}},
41+
{"TestCase", []int{1, 2, 3}, []int{1, 2, 3}},
42+
{"TestCase", []int{}, []int{}},
43+
}
44+
45+
// 开始测试
46+
for i, c := range cases {
47+
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
48+
Solution1(c.inputs)
49+
if !reflect.DeepEqual(c.inputs, c.expect) {
50+
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
51+
c.expect, c.inputs, c.inputs)
52+
}
53+
})
54+
}
55+
}
56+
57+
// 压力测试
3458
func BenchmarkSolution(b *testing.B) {
3559
}
3660

37-
// 使用案列
61+
// 使用案列
3862
func ExampleSolution() {
3963
}

0 commit comments

Comments
 (0)