Skip to content

Commit 7bfa08e

Browse files
committed
feat: leetcode 942
1 parent 2c116e8 commit 7bfa08e

File tree

8 files changed

+116
-1
lines changed

8 files changed

+116
-1
lines changed

Diff for: 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/-进度:360-green" alt="进度:360">
5+
<img src="https://img.shields.io/badge/-进度:361-green" alt="进度:361">
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>

Diff for: assets/data/categories.json

+6
Original file line numberDiff line numberDiff line change
@@ -2662,6 +2662,12 @@
26622662
"path": "./problemset/patching-array/README.md",
26632663
"difficulty": "困难"
26642664
},
2665+
{
2666+
"id": "942",
2667+
"title": "增减字符串匹配",
2668+
"path": "./problemset/di-string-match/README.md",
2669+
"difficulty": "简单"
2670+
},
26652671
{
26662672
"id": "1663",
26672673
"title": "具有给定数值的最小字符串",

Diff for: assets/data/topics.json

+10
Original file line numberDiff line numberDiff line change
@@ -3219,6 +3219,16 @@
32193219
"url": "https://leetcode-cn.com/problems/reorder-data-in-log-files/",
32203220
"path": "./problemset/reorder-data-in-log-files/README.md"
32213221
},
3222+
{
3223+
"id": "942",
3224+
"title": {
3225+
"cn": "增减字符串匹配",
3226+
"en": "di-string-match"
3227+
},
3228+
"difficulty": "简单",
3229+
"url": "https://leetcode.cn/problems/di-string-match/",
3230+
"path": "./problemset/di-string-match/README.md"
3231+
},
32223232
{
32233233
"id": "954",
32243234
"title": {

Diff for: assets/docs/CATEGORIES.md

+1
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,7 @@
514514
| 122. [买卖股票的最佳时机 II](../../problemset/best-time-to-buy-and-sell-stock-2/README.md) | 中等 |
515515
| 300. [最长递增子序列](../../problemset/longest-increasing-subsequence/README.md) | 中等 |
516516
| 330. [按要求补齐数组](../../problemset/patching-array/README.md) | 困难 |
517+
| 942. [增减字符串匹配](../../problemset/di-string-match/README.md) | 简单 |
517518
| 1663. [具有给定数值的最小字符串](../../problemset/smallest-string-with-a-given-numeric-value/README.md) | 中等 |
518519
| 2038. [如果相邻两个颜色均相同则删除当前颜色](../../problemset/remove-colored-pieces-if-both-neighbors-are-the-same-color/README.md) | 中等 |
519520

Diff for: assets/docs/TOPICS.md

+2
Original file line numberDiff line numberDiff line change
@@ -644,6 +644,8 @@
644644

645645
[937. 重新排列日志文件](../../problemset/reorder-data-in-log-files/README.md)
646646

647+
[942. 增减字符串匹配](../../problemset/di-string-match/README.md)
648+
647649
[954. 二倍数对数组](../../problemset/array-of-doubled-pairs/README.md)
648650

649651
[969. 煎饼排序](../../problemset/pancake-sorting/README.md)

Diff for: problemset/di-string-match/README.md

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# 增减字符串匹配
2+
3+
> 难度:简单
4+
>
5+
> https://leetcode.cn/problems/di-string-match/
6+
7+
## 题目
8+
9+
由范围 `[0,n]` 内所有整数组成的 `n + 1` 个整数的排列序列可以表示为长度为 `n` 的字符串 `s` ,其中:
10+
11+
- 如果 `perm[i] < perm[i + 1]` ,那么 `s[i] == 'I'` 
12+
- 如果 `perm[i] > perm[i + 1]` ,那么 `s[i] == 'D'` 
13+
14+
给定一个字符串 `s` ,重构排列 `perm` 并返回它。如果有多个有效排列`perm`,则返回其中 **任何一个**
15+
16+
### 示例 
17+
18+
#### 示例 1:
19+
20+
```
21+
输入:s = "IDID"
22+
输出:[0,4,1,3,2]
23+
```
24+
25+
#### 示例 2:
26+
27+
```
28+
输入:s = "III"
29+
输出:[0,1,2,3]
30+
```
31+
32+
#### 示例 3:
33+
34+
```
35+
输入:s = "DDI"
36+
输出:[3,2,0,1]
37+
```
38+
39+
## 解题
40+
41+
```ts
42+
/**
43+
* 贪心算法
44+
* @desc 时间复杂度 O(N) 空间复杂度 O(1)
45+
* @param s
46+
* @returns
47+
*/
48+
export function diStringMatch(s: string): number[] {
49+
const len = s.length
50+
const ans = new Array(len + 1).fill(0)
51+
let low = 0
52+
let high = len
53+
54+
for (let i = 0; i < len; i++)
55+
ans[i] = s[i] === 'I' ? low++ : high--
56+
57+
ans[len] = low
58+
59+
return ans
60+
}
61+
```

Diff for: problemset/di-string-match/index.spec.ts

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { describe, expect, it } from 'vitest'
2+
import { diStringMatch } from '.'
3+
4+
describe('增减字符串匹配', () => {
5+
testCase(diStringMatch)
6+
})
7+
8+
function testCase(fn: (s: string) => number[]) {
9+
it.each([
10+
['IDID', [0, 4, 1, 3, 2]],
11+
['III', [0, 1, 2, 3]],
12+
['DDI', [3, 2, 0, 1]],
13+
])('示例%#', (s, expected) => {
14+
expect(fn(s)).toStrictEqual(expected)
15+
})
16+
}

Diff for: problemset/di-string-match/index.ts

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* 贪心算法
3+
* @desc 时间复杂度 O(N) 空间复杂度 O(1)
4+
* @param s
5+
* @returns
6+
*/
7+
export function diStringMatch(s: string): number[] {
8+
const len = s.length
9+
const ans = new Array(len + 1).fill(0)
10+
let low = 0
11+
let high = len
12+
13+
for (let i = 0; i < len; i++)
14+
ans[i] = s[i] === 'I' ? low++ : high--
15+
16+
ans[len] = low
17+
18+
return ans
19+
}

0 commit comments

Comments
 (0)