Skip to content

Commit 6d89bd6

Browse files
committed
feat: leetcode 1089
1 parent 87a9be9 commit 6d89bd6

File tree

8 files changed

+112
-1
lines changed

8 files changed

+112
-1
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<p align="center">
44
<!-- TOPICS COUNT START -->
5-
<img src="https://img.shields.io/badge/-进度:404-green" alt="进度:404">
5+
<img src="https://img.shields.io/badge/-进度:405-green" alt="进度:405">
66
<!-- TOPICS COUNT END -->
77
<a href="./assets/docs/TOPICS.md"><img src="https://img.shields.io/badge/-题库目录-blue" alt="题库目录"></a>
88
<a href="./assets/docs/CATEGORIES.md"><img src="https://img.shields.io/badge/-题库分类-red" alt="题库分类"></a>

assets/data/categories.json

+6
Original file line numberDiff line numberDiff line change
@@ -2003,6 +2003,12 @@
20032003
"path": "./problemset/pancake-sorting/README.md",
20042004
"difficulty": "中等"
20052005
},
2006+
{
2007+
"id": "1089",
2008+
"title": "复写零",
2009+
"path": "./problemset/duplicate-zeros/README.md",
2010+
"difficulty": "简单"
2011+
},
20062012
{
20072013
"id": "1342",
20082014
"title": "将数字变成 0 的操作次数",

assets/data/topics.json

+10
Original file line numberDiff line numberDiff line change
@@ -3649,6 +3649,16 @@
36493649
"url": "https://leetcode.cn/problems/height-checker/",
36503650
"path": "./problemset/height-checker/README.md"
36513651
},
3652+
{
3653+
"id": "1089",
3654+
"title": {
3655+
"cn": "复写零",
3656+
"en": "duplicate-zeros"
3657+
},
3658+
"difficulty": "简单",
3659+
"url": "https://leetcode.cn/problems/duplicate-zeros/",
3660+
"path": "./problemset/duplicate-zeros/README.md"
3661+
},
36523662
{
36533663
"id": "1189",
36543664
"title": {

assets/docs/CATEGORIES.md

+1
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,7 @@
375375
| 944. [删列造序](../../problemset/delete-columns-to-make-sorted/README.md) | 简单 |
376376
| 953. [验证外星语词典](../../problemset/verifying-an-alien-dictionary/README.md) | 简单 |
377377
| 969. [煎饼排序](../../problemset/pancake-sorting/README.md) | 中等 |
378+
| 1089. [复写零](../../problemset/duplicate-zeros/README.md) | 简单 |
378379
| 1342. [将数字变成 0 的操作次数](../../problemset/number-of-steps-to-reduce-a-number-to-zero/README.md) | 简单 |
379380
| 1606. [找到处理最多请求的服务器](../../problemset/find-servers-that-handled-most-number-of-requests/README.md) | 困难 |
380381
| 1672. [最富有客户的资产总量](../../problemset/richest-customer-wealth/README.md) | 简单 |

assets/docs/TOPICS.md

+2
Original file line numberDiff line numberDiff line change
@@ -730,6 +730,8 @@
730730

731731
[1051. 高度检查器](../../problemset/height-checker/README.md)
732732

733+
[1089. 复写零](../../problemset/duplicate-zeros/README.md)
734+
733735
[1189. “气球” 的最大数量](../../problemset/maximum-number-of-balloons/README.md)
734736

735737
[1305. 两颗二叉搜索树中的所有元素](../../problemset/all-elements-in-two-binary-search-trees/README.md)

problemset/duplicate-zeros/README.md

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# 复写零
2+
3+
> 难度:简单
4+
>
5+
> https://leetcode.cn/problems/duplicate-zeros/
6+
7+
## 题目
8+
9+
给你一个长度固定的整数数组 `arr`,请你将该数组中出现的每个零都复写一遍,并将其余的元素向右平移。
10+
11+
注意:请不要在超过该数组长度的位置写入元素。
12+
13+
要求:请对输入的数组 **就地** 进行上述修改,不要从函数返回任何东西。
14+
15+
### 示例
16+
17+
#### 示例 1:
18+
19+
```
20+
输入:[1,0,2,3,0,4,5,0]
21+
输出:null
22+
解释:调用函数后,输入的数组将被修改为:[1,0,0,2,3,0,0,4]
23+
```
24+
25+
#### 示例 2:
26+
27+
```
28+
输入:[1,2,3]
29+
输出:null
30+
解释:调用函数后,输入的数组将被修改为:[1,2,3]
31+
```
32+
33+
## 解题
34+
35+
```ts
36+
/**
37+
* 遍历
38+
* @desc 时间复杂度 O(N) 空间复杂度 O(1)
39+
* @param arr
40+
*/
41+
export function duplicateZeros(arr: number[]): void {
42+
const len = arr.length
43+
let i = 0
44+
while (i < len) {
45+
if (arr[i] === 0) {
46+
arr.pop()
47+
arr.splice(i, 0, 0)
48+
i++
49+
}
50+
i++
51+
}
52+
}
53+
```
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { describe, expect, it } from 'vitest'
2+
import { duplicateZeros } from '.'
3+
4+
describe('复写零', () => {
5+
testCase(duplicateZeros)
6+
})
7+
8+
function testCase(fn: (arr: number[]) => void) {
9+
it.each([
10+
[
11+
[1, 0, 2, 3, 0, 4, 5, 0],
12+
[1, 0, 0, 2, 3, 0, 0, 4],
13+
],
14+
[
15+
[1, 2, 3],
16+
[1, 2, 3],
17+
],
18+
])('示例%#', (arr, expected) => {
19+
fn(arr)
20+
expect(arr).toStrictEqual(expected)
21+
})
22+
}

problemset/duplicate-zeros/index.ts

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* 遍历
3+
* @desc 时间复杂度 O(N) 空间复杂度 O(1)
4+
* @param arr
5+
*/
6+
export function duplicateZeros(arr: number[]): void {
7+
const len = arr.length
8+
let i = 0
9+
while (i < len) {
10+
if (arr[i] === 0) {
11+
arr.pop()
12+
arr.splice(i, 0, 0)
13+
i++
14+
}
15+
i++
16+
}
17+
}

0 commit comments

Comments
 (0)