Skip to content
This repository was archived by the owner on Sep 20, 2023. It is now read-only.

Commit fed028f

Browse files
aQuaaQua
aQua
authored and
aQua
committed
97 低估了题目的难度,删除重写。
1 parent 8117ad6 commit fed028f

File tree

3 files changed

+110
-0
lines changed

3 files changed

+110
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# [97. Interleaving String](https://leetcode.com/problems/interleaving-string/)
2+
3+
## 题目
4+
Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2.
5+
```
6+
For example,
7+
Given:
8+
s1 = "aabcc",
9+
s2 = "dbbca",
10+
11+
When s3 = "aadbbcbcac", return true.
12+
When s3 = "aadbbbaccc", return false.
13+
```
14+
15+
## 解题思路
16+
s3 能否由 s1 和 s2,交替融合而生。
17+
18+
见程序注释
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package Problem0097
2+
3+
func isInterleave(s1 string, s2 string, s3 string) bool {
4+
m, n := len(s1), len(s2)
5+
if m+n != len(s3) {
6+
return false
7+
}
8+
9+
dp := make([]bool, m+n+1)
10+
dp[0] = true
11+
i, j, k := 0, 0, 0
12+
for i <= m && j <= n && k < m+n+1 {
13+
switch {
14+
case i < m && s1[i] == s3[k]:
15+
dp[k] = true
16+
i++
17+
k++
18+
case j < n && s2[j] == s3[k]:
19+
dp[k] = true
20+
j++
21+
k++
22+
default:
23+
return false
24+
}
25+
if i == m && j == n {
26+
return true
27+
}
28+
}
29+
return false
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package Problem0097
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
type question struct {
11+
para
12+
ans
13+
}
14+
15+
// para 是参数
16+
type para struct {
17+
s1 string
18+
s2 string
19+
s3 string
20+
}
21+
22+
// ans 是答案
23+
type ans struct {
24+
one bool
25+
}
26+
27+
func Test_Problem0097(t *testing.T) {
28+
ast := assert.New(t)
29+
30+
qs := []question{
31+
32+
question{
33+
para{
34+
"aabcc",
35+
"dbbca",
36+
"aadbbcbcac",
37+
},
38+
ans{
39+
true,
40+
},
41+
},
42+
43+
question{
44+
para{
45+
"aabcc",
46+
"dbbca",
47+
"aadbbbaccc",
48+
},
49+
ans{
50+
false,
51+
},
52+
},
53+
// 如需多个测试,可以复制上方元素。
54+
}
55+
56+
for _, q := range qs {
57+
a, p := q.ans, q.para
58+
fmt.Printf("~~%v~~\n", p)
59+
60+
ast.Equal(a.one, isInterleave(p.s1, p.s2, p.s3), "输入:%v", p)
61+
}
62+
}

0 commit comments

Comments
 (0)