Skip to content

Add solution and test-cases for problem 1089 #1102

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 11 additions & 14 deletions leetcode/1001-1100/1089.Duplicate-Zeros/README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,25 @@
# [1089.Duplicate Zeros][title]

> [!WARNING|style:flat]
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)

## Description
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: a = "11", b = "1"
Output: "100"
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:**

### 思路1
> ...
Duplicate Zeros
```go
```

Input: arr = [1,2,3]
Output: [1,2,3]
Explanation: After calling your function, the input array is modified to: [1,2,3]
```

## 结语

Expand Down
14 changes: 14 additions & 0 deletions leetcode/1001-1100/1089.Duplicate-Zeros/Solution.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,17 @@ func Solution(arr []int) {
}
}
}

func Solution1(arr []int) {
tmp := make([]int, len(arr))
index, i := 0, 0
for ; i < len(arr) && index < len(arr); i++ {
if arr[i] != 0 {
tmp[index] = arr[i]
index++
continue
}
index += 2
}
copy(arr, tmp)
}
28 changes: 26 additions & 2 deletions leetcode/1001-1100/1089.Duplicate-Zeros/Solution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,34 @@ func TestSolution(t *testing.T) {
}
}

// 压力测试
func TestSolution1(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs []int
expect []int
}{
{"TestCase", []int{1, 0, 2, 3, 0, 4, 5, 0}, []int{1, 0, 0, 2, 3, 0, 0, 4}},
{"TestCase", []int{1, 2, 3}, []int{1, 2, 3}},
{"TestCase", []int{}, []int{}},
}

// 开始测试
for i, c := range cases {
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
Solution1(c.inputs)
if !reflect.DeepEqual(c.inputs, c.expect) {
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
c.expect, c.inputs, c.inputs)
}
})
}
}

// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}
Loading