diff --git a/leetcode/1001-1100/1089.Duplicate-Zeros/README.md b/leetcode/1001-1100/1089.Duplicate-Zeros/README.md index 824bb0373..0c9835889 100644 --- a/leetcode/1001-1100/1089.Duplicate-Zeros/README.md +++ b/leetcode/1001-1100/1089.Duplicate-Zeros/README.md @@ -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] +``` ## 结语 diff --git a/leetcode/1001-1100/1089.Duplicate-Zeros/Solution.go b/leetcode/1001-1100/1089.Duplicate-Zeros/Solution.go index 0928a389b..b862b4a92 100644 --- a/leetcode/1001-1100/1089.Duplicate-Zeros/Solution.go +++ b/leetcode/1001-1100/1089.Duplicate-Zeros/Solution.go @@ -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) +} diff --git a/leetcode/1001-1100/1089.Duplicate-Zeros/Solution_test.go b/leetcode/1001-1100/1089.Duplicate-Zeros/Solution_test.go index 806aebc40..b07b482c2 100644 --- a/leetcode/1001-1100/1089.Duplicate-Zeros/Solution_test.go +++ b/leetcode/1001-1100/1089.Duplicate-Zeros/Solution_test.go @@ -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() { }