Skip to content

Commit

Permalink
Add solution and test-cases for problem 1089
Browse files Browse the repository at this point in the history
  • Loading branch information
0xff-dev committed Jan 23, 2025
1 parent d569e0c commit c416a5d
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 16 deletions.
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() {
}

0 comments on commit c416a5d

Please sign in to comment.