Skip to content

Commit 8d88f53

Browse files
committed
[E:69/535, M:70/978, H:8/390] add No: 100297: 正则表达式匹配 LCOF
1 parent 5f1a6de commit 8d88f53

File tree

9 files changed

+348
-0
lines changed

9 files changed

+348
-0
lines changed

.leetcode.db

0 Bytes
Binary file not shown.
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
## [正则表达式匹配](https://leetcode-cn.com/problems/zheng-ze-biao-da-shi-pi-pei-lcof/)
2+
3+
请实现一个函数用来匹配包含`'. '``'*'`的正则表达式。模式中的字符`'.'`表示任意一个字符,而`'*'`表示它前面的字符可以出现任意次(含0次)。在本题中,匹配是指字符串的所有字符匹配整个模式。例如,字符串`"aaa"`与模式`"a.a"``"ab*ac*a"`匹配,但与`"aa.a"``"ab*a"`均不匹配。
4+
5+
**示例 1:**
6+
7+
`**输入:**
8+
s = "aa"
9+
p = "a"
10+
**输出:** false
11+
**解释:** "a" 无法匹配 "aa" 整个字符串。
12+
`
13+
14+
**示例 2:**
15+
16+
`**输入:**
17+
s = "aa"
18+
p = "a*"
19+
**输出:** true
20+
**解释:** 因为 '*' 代表可以匹配零个或多个前面的那一个元素, 在这里前面的元素就是 'a'。因此,字符串 "aa" 可被视为 'a' 重复了一次。
21+
`
22+
23+
**示例 3:**
24+
25+
`**输入:**
26+
s = "ab"
27+
p = ".*"
28+
**输出:** true
29+
**解释:** ".*" 表示可匹配零个或多个('*')任意字符('.')。
30+
`
31+
32+
**示例 4:**
33+
34+
`**输入:**
35+
s = "aab"
36+
p = "c*a*b"
37+
**输出:** true
38+
**解释:** 因为 '*' 表示零个或多个,这里 'c' 为 0 个, 'a' 被重复一次。因此可以匹配字符串 "aab"。
39+
`
40+
41+
**示例 5:**
42+
43+
`**输入:**
44+
s = "mississippi"
45+
p = "mis*is*p*."
46+
**输出:** false`
47+
48+
* `s` 可能为空,且只包含从 `a-z` 的小写字母。
49+
* `p` 可能为空,且只包含从 `a-z` 的小写字母以及字符 `.` 和 `*`,无连续的 `'*'`
50+
51+
注意:本题与主站 10 题相同:[https://leetcode-cn.com/problems/regular-expression-matching/](https://leetcode-cn.com/problems/regular-expression-matching/)
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"reflect"
7+
"time"
8+
9+
"github.com/gladmo/leetcode/questions/serial/hard/100297/golang/solution"
10+
"github.com/gladmo/leetcode/leet"
11+
)
12+
13+
func main() {
14+
/*
15+
16+
"aa"
17+
"a"
18+
19+
*/
20+
21+
tests := []struct {
22+
name string
23+
input [][]int
24+
want bool
25+
}{
26+
{
27+
name: "test-[[1],[2],[3],[]]",
28+
input: [][]int{
29+
{1},
30+
{2},
31+
{3},
32+
{},
33+
},
34+
want: true,
35+
},
36+
}
37+
38+
testLog := leet.NewTestLog(len(tests))
39+
defer testLog.Render()
40+
41+
timeoutDuration := time.Second * 2
42+
43+
for idx, test := range tests {
44+
// 超时检测
45+
got := test.want
46+
timeout := leet.Timeout(timeoutDuration, func(ctx context.Context, cancel context.CancelFunc) {
47+
got = solution.Export(test.input)
48+
cancel()
49+
})
50+
51+
if timeout {
52+
testLog.Fail(idx+1, test.name, "timeout")
53+
continue
54+
}
55+
56+
if !reflect.DeepEqual(test.want, got) {
57+
testLog.Fail(idx+1, test.name, fmt.Sprintf("want: %v, got %v.", test.want, got))
58+
continue
59+
}
60+
61+
testLog.Pass(idx+1, test.name)
62+
}
63+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package solution
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"runtime/debug"
7+
)
8+
9+
func Export(s string, p string) bool {
10+
defer func() {
11+
if r := recover(); r != nil {
12+
fmt.Println("Params: ", s, p)
13+
fmt.Println("Panic:", r)
14+
fmt.Println()
15+
debug.PrintStack()
16+
os.Exit(0)
17+
}
18+
}()
19+
20+
return isMatch(s, p)
21+
}
22+
23+
/****************************************************/
24+
/******** 以下为 Leetcode 示例部分(提交PR请还原) *******/
25+
/******** 使用 (./leetcode clear) 初始化所有问题 *******/
26+
/****************************************************/
27+
28+
func isMatch(s string, p string) bool {
29+
30+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package solution
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"runtime/debug"
7+
)
8+
9+
func Export(s string, p string) bool {
10+
defer func() {
11+
if r := recover(); r != nil {
12+
fmt.Println("Params: ", s, p)
13+
fmt.Println("Panic:", r)
14+
fmt.Println()
15+
debug.PrintStack()
16+
os.Exit(0)
17+
}
18+
}()
19+
20+
return isMatch(s, p)
21+
}
22+
23+
/****************************************************/
24+
/******** 以下为 Leetcode 示例部分(提交PR请还原) *******/
25+
/******** 使用 (./leetcode clear) 初始化所有问题 *******/
26+
/****************************************************/
27+
28+
func isMatch(s string, p string) bool {
29+
30+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
## [正则表达式匹配](https://leetcode-cn.com/problems/zheng-ze-biao-da-shi-pi-pei-lcof/)
2+
3+
请实现一个函数用来匹配包含`'. '``'*'`的正则表达式。模式中的字符`'.'`表示任意一个字符,而`'*'`表示它前面的字符可以出现任意次(含0次)。在本题中,匹配是指字符串的所有字符匹配整个模式。例如,字符串`"aaa"`与模式`"a.a"``"ab*ac*a"`匹配,但与`"aa.a"``"ab*a"`均不匹配。
4+
5+
**示例 1:**
6+
7+
`**输入:**
8+
s = "aa"
9+
p = "a"
10+
**输出:** false
11+
**解释:** "a" 无法匹配 "aa" 整个字符串。
12+
`
13+
14+
**示例 2:**
15+
16+
`**输入:**
17+
s = "aa"
18+
p = "a*"
19+
**输出:** true
20+
**解释:** 因为 '*' 代表可以匹配零个或多个前面的那一个元素, 在这里前面的元素就是 'a'。因此,字符串 "aa" 可被视为 'a' 重复了一次。
21+
`
22+
23+
**示例 3:**
24+
25+
`**输入:**
26+
s = "ab"
27+
p = ".*"
28+
**输出:** true
29+
**解释:** ".*" 表示可匹配零个或多个('*')任意字符('.')。
30+
`
31+
32+
**示例 4:**
33+
34+
`**输入:**
35+
s = "aab"
36+
p = "c*a*b"
37+
**输出:** true
38+
**解释:** 因为 '*' 表示零个或多个,这里 'c' 为 0 个, 'a' 被重复一次。因此可以匹配字符串 "aab"。
39+
`
40+
41+
**示例 5:**
42+
43+
`**输入:**
44+
s = "mississippi"
45+
p = "mis*is*p*."
46+
**输出:** false`
47+
48+
* `s` 可能为空,且只包含从 `a-z` 的小写字母。
49+
* `p` 可能为空,且只包含从 `a-z` 的小写字母以及字符 `.` 和 `*`,无连续的 `'*'`
50+
51+
注意:本题与主站 10 题相同:[https://leetcode-cn.com/problems/regular-expression-matching/](https://leetcode-cn.com/problems/regular-expression-matching/)
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"reflect"
7+
"time"
8+
9+
"github.com/gladmo/leetcode/questions/tags/动态规划/hard/zheng-ze-biao-da-shi-pi-pei-lcof/golang/solution"
10+
"github.com/gladmo/leetcode/leet"
11+
)
12+
13+
func main() {
14+
/*
15+
16+
"aa"
17+
"a"
18+
19+
*/
20+
21+
tests := []struct {
22+
name string
23+
input [][]int
24+
want bool
25+
}{
26+
{
27+
name: "test-[[1],[2],[3],[]]",
28+
input: [][]int{
29+
{1},
30+
{2},
31+
{3},
32+
{},
33+
},
34+
want: true,
35+
},
36+
}
37+
38+
testLog := leet.NewTestLog(len(tests))
39+
defer testLog.Render()
40+
41+
timeoutDuration := time.Second * 2
42+
43+
for idx, test := range tests {
44+
// 超时检测
45+
got := test.want
46+
timeout := leet.Timeout(timeoutDuration, func(ctx context.Context, cancel context.CancelFunc) {
47+
got = solution.Export(test.input)
48+
cancel()
49+
})
50+
51+
if timeout {
52+
testLog.Fail(idx+1, test.name, "timeout")
53+
continue
54+
}
55+
56+
if !reflect.DeepEqual(test.want, got) {
57+
testLog.Fail(idx+1, test.name, fmt.Sprintf("want: %v, got %v.", test.want, got))
58+
continue
59+
}
60+
61+
testLog.Pass(idx+1, test.name)
62+
}
63+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package solution
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"runtime/debug"
7+
)
8+
9+
func Export(s string, p string) bool {
10+
defer func() {
11+
if r := recover(); r != nil {
12+
fmt.Println("Params: ", s, p)
13+
fmt.Println("Panic:", r)
14+
fmt.Println()
15+
debug.PrintStack()
16+
os.Exit(0)
17+
}
18+
}()
19+
20+
return isMatch(s, p)
21+
}
22+
23+
/****************************************************/
24+
/******** 以下为 Leetcode 示例部分(提交PR请还原) *******/
25+
/******** 使用 (./leetcode clear) 初始化所有问题 *******/
26+
/****************************************************/
27+
28+
func isMatch(s string, p string) bool {
29+
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package solution
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"runtime/debug"
7+
)
8+
9+
func Export(s string, p string) bool {
10+
defer func() {
11+
if r := recover(); r != nil {
12+
fmt.Println("Params: ", s, p)
13+
fmt.Println("Panic:", r)
14+
fmt.Println()
15+
debug.PrintStack()
16+
os.Exit(0)
17+
}
18+
}()
19+
20+
return isMatch(s, p)
21+
}
22+
23+
/****************************************************/
24+
/******** 以下为 Leetcode 示例部分(提交PR请还原) *******/
25+
/******** 使用 (./leetcode clear) 初始化所有问题 *******/
26+
/****************************************************/
27+
28+
func isMatch(s string, p string) bool {
29+
30+
}

0 commit comments

Comments
 (0)