Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 18 additions & 13 deletions leetcode/901-1000/0942.DI-String-Match/README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,33 @@
# [942.DI String Match][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
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:

- `s[i] == 'I'` if `perm[i] < perm[i + 1]`, and
- `s[i] == 'D'` if `perm[i] > perm[i + 1]`.

Given a string `s`, reconstruct the permutation `perm` and return it. If there are multiple valid permutations perm, return **any of them**.

**Example 1:**

```
Input: a = "11", b = "1"
Output: "100"
Input: s = "IDID"
Output: [0,4,1,3,2]
```

## 题意
> ...

## 题解
**Example 2:**

### 思路1
> ...
DI String Match
```go
```
Input: s = "III"
Output: [0,1,2,3]
```

**Example 3:**

```
Input: s = "DDI"
Output: [3,2,0,1]
```

## 结语

Expand Down
27 changes: 25 additions & 2 deletions leetcode/901-1000/0942.DI-String-Match/Solution.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,28 @@
package Solution

func Solution(x bool) bool {
return x
func Solution(s string) []int {
res := make([]int, len(s)+1)
for i := range res {
res[i] = -1
}

index := 0
for i := 0; i < len(s); i++ {
if s[i] == 'D' {
continue
}
res[i] = index
index++
for pre := i - 1; pre >= 0 && res[pre] == -1; pre-- {
res[pre] = index
index++
}
}
res[len(s)] = index
index++
for pre := len(s) - 1; pre >= 0 && res[pre] == -1; pre-- {
res[pre] = index
index++
}
return res
}
14 changes: 7 additions & 7 deletions leetcode/901-1000/0942.DI-String-Match/Solution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
inputs string
expect []int
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", "IDID", []int{0, 2, 1, 4, 3}},
{"TestCase2", "III", []int{0, 1, 2, 3}},
{"TestCase3", "DDI", []int{2, 1, 0, 3}},
}

// 开始测试
Expand All @@ -30,10 +30,10 @@ func TestSolution(t *testing.T) {
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}
Loading