Skip to content

Commit 222a9f8

Browse files
committed
Add solution and test-cases for problem 942
1 parent d569e0c commit 222a9f8

File tree

3 files changed

+50
-22
lines changed

3 files changed

+50
-22
lines changed

leetcode/901-1000/0942.DI-String-Match/README.md

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,33 @@
11
# [942.DI String Match][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
A permutation `perm` of `n + 1` integers of all the integers in the range `[0, n]` can be represented as a string `s` of length `n` where:
5+
6+
- `s[i] == 'I'` if `perm[i] < perm[i + 1]`, and
7+
- `s[i] == 'D'` if `perm[i] > perm[i + 1]`.
8+
9+
Given a string `s`, reconstruct the permutation `perm` and return it. If there are multiple valid permutations perm, return **any of them**.
710

811
**Example 1:**
912

1013
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
14+
Input: s = "IDID"
15+
Output: [0,4,1,3,2]
1316
```
1417

15-
## 题意
16-
> ...
17-
18-
## 题解
18+
**Example 2:**
1919

20-
### 思路1
21-
> ...
22-
DI String Match
23-
```go
2420
```
21+
Input: s = "III"
22+
Output: [0,1,2,3]
23+
```
24+
25+
**Example 3:**
2526

27+
```
28+
Input: s = "DDI"
29+
Output: [3,2,0,1]
30+
```
2631

2732
## 结语
2833

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,28 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(s string) []int {
4+
res := make([]int, len(s)+1)
5+
for i := range res {
6+
res[i] = -1
7+
}
8+
9+
index := 0
10+
for i := 0; i < len(s); i++ {
11+
if s[i] == 'D' {
12+
continue
13+
}
14+
res[i] = index
15+
index++
16+
for pre := i - 1; pre >= 0 && res[pre] == -1; pre-- {
17+
res[pre] = index
18+
index++
19+
}
20+
}
21+
res[len(s)] = index
22+
index++
23+
for pre := len(s) - 1; pre >= 0 && res[pre] == -1; pre-- {
24+
res[pre] = index
25+
index++
26+
}
27+
return res
528
}

leetcode/901-1000/0942.DI-String-Match/Solution_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs string
14+
expect []int
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", "IDID", []int{0, 2, 1, 4, 3}},
17+
{"TestCase2", "III", []int{0, 1, 2, 3}},
18+
{"TestCase3", "DDI", []int{2, 1, 0, 3}},
1919
}
2020

2121
// 开始测试
@@ -30,10 +30,10 @@ func TestSolution(t *testing.T) {
3030
}
3131
}
3232

33-
// 压力测试
33+
// 压力测试
3434
func BenchmarkSolution(b *testing.B) {
3535
}
3636

37-
// 使用案列
37+
// 使用案列
3838
func ExampleSolution() {
3939
}

0 commit comments

Comments
 (0)