Skip to content

Commit 8e239b8

Browse files
aQuaaQua
aQua
authored and
aQua
committed
44 finish
1 parent 06e267c commit 8e239b8

File tree

6 files changed

+260
-0
lines changed

6 files changed

+260
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# [44. Wildcard Matching](https://leetcode.com/problems/wildcard-matching/)
2+
3+
## 题目
4+
Implement wildcard pattern matching with support for '?' and '*'.
5+
6+
'?' Matches any single character.
7+
'*' Matches any sequence of characters (including the empty sequence).
8+
9+
The matching should cover the entire input string (not partial).
10+
11+
The function prototype should be:
12+
bool isMatch(const char *s, const char *p)
13+
14+
Some examples:
15+
isMatch("aa","a") → false
16+
isMatch("aa","aa") → true
17+
isMatch("aaa","aa") → false
18+
isMatch("aa", "*") → true
19+
isMatch("aa", "a*") → true
20+
isMatch("ab", "?*") → true
21+
isMatch("aab", "c*a*b") → false
22+
23+
## 解题思路
24+
注意审题:
25+
1. '?' 可以匹配任意一个字符,但是不能匹配空字符""
26+
1. '*' 可以任意多个字符,包括""
27+
28+
见程序注释
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package Problem0044
2+
3+
func isMatch(s string, p string) bool {
4+
ls, lp := len(s), len(p)
5+
6+
dp := [][]bool{}
7+
for i := 0; i < ls+1; i++ {
8+
rt := make([]bool, lp+1)
9+
dp = append(dp, rt)
10+
}
11+
// dp[i][j] == true 意味着,s[:i+1] 可以和 p[:j+1] 匹配
12+
dp[0][0] = true
13+
14+
for j := 1; j <= lp; j++ {
15+
if p[j-1] == '*' {
16+
// 当 p[j-1] == '*' 时
17+
// 只要前面的匹配,dp[0][j] 就匹配
18+
// 一旦 p[j-1] != '*',后面的 dp[0][j] 就都为 false
19+
dp[0][j] = dp[0][j-1]
20+
}
21+
}
22+
23+
for i := 1; i <= ls; i++ {
24+
for j := 1; j <= lp; j++ {
25+
if p[j-1] != '*' {
26+
// 当 p[j-1] != '*' 时
27+
// 单个字符要匹配,并且之前的字符串也要匹配。
28+
dp[i][j] = (p[j-1] == s[i-1] || p[j-1] == '?') && dp[i-1][j-1]
29+
} else {
30+
// 当 p[j-1] == '*' 时
31+
// 要么,dp[i-1][j] == true,意味着,
32+
// 当 s[:i] 与 p[:j+1] 匹配,且p[j] == '*' 的时候,
33+
// s[:i] 后接任意字符串的 s[:i+1] 仍与 p[:j+1] 匹配。
34+
// 要么,dp[i][j-1] == true,意味着,
35+
// 当 s[:i+1] 与 p[:j] 匹配后
36+
// 在 p[:j] 后添加'*',s[:i+1] 与 p[:j+1] 任然匹配
37+
// 因为, '*' 可以匹配空字符。
38+
dp[i][j] = dp[i-1][j] || dp[i][j-1]
39+
}
40+
}
41+
}
42+
return dp[ls][lp]
43+
}
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
package Problem0044
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+
s string
18+
p string
19+
}
20+
21+
// ans 是答案
22+
type ans struct {
23+
one bool
24+
}
25+
26+
func Test_Problem0044(t *testing.T) {
27+
ast := assert.New(t)
28+
29+
qs := []question{
30+
31+
question{
32+
para{
33+
"aa",
34+
"a",
35+
},
36+
ans{
37+
false,
38+
},
39+
},
40+
41+
question{
42+
para{
43+
"aa",
44+
"aa",
45+
},
46+
ans{
47+
true,
48+
},
49+
},
50+
51+
question{
52+
para{
53+
"aaa",
54+
"aa",
55+
},
56+
ans{
57+
false,
58+
},
59+
},
60+
61+
question{
62+
para{
63+
"aa",
64+
"*",
65+
},
66+
ans{
67+
true,
68+
},
69+
},
70+
71+
question{
72+
para{
73+
"aa",
74+
"a*",
75+
},
76+
ans{
77+
true,
78+
},
79+
},
80+
81+
question{
82+
para{
83+
"ab",
84+
"?*",
85+
},
86+
ans{
87+
true,
88+
},
89+
},
90+
91+
question{
92+
para{
93+
"aab",
94+
"c*a*b",
95+
},
96+
ans{
97+
false,
98+
},
99+
},
100+
101+
question{
102+
para{
103+
"ab",
104+
"*",
105+
},
106+
ans{
107+
true,
108+
},
109+
},
110+
// 如需多个测试,可以复制上方元素。
111+
}
112+
113+
for _, q := range qs {
114+
a, p := q.ans, q.para
115+
fmt.Printf("~~%v~~\n", p)
116+
117+
ast.Equal(a.one, isMatch(p.s, p.p), "输入:%v", p)
118+
}
119+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# [58. Length of Last Word](https://leetcode.com/problems/length-of-last-word/)
2+
3+
## 题目
4+
Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the length of last word in the string.
5+
6+
If the last word does not exist, return 0.
7+
8+
Note: A word is defined as a character sequence consists of non-space characters only.
9+
10+
11+
For example,
12+
Given s = "Hello World",
13+
return 5.
14+
15+
## 解题思路
16+
17+
见程序注释
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package Problem0058
2+
3+
func lengthOfLastWord(s string) int {
4+
5+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package Problem0058
2+
3+
import (
4+
"testing"
5+
"fmt"
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+
s string
18+
}
19+
20+
// ans 是答案
21+
type ans struct {
22+
one int
23+
}
24+
25+
func Test_Problem0058(t *testing.T) {
26+
ast := assert.New(t)
27+
28+
qs := []question{
29+
30+
question{
31+
para{
32+
,
33+
},
34+
ans{
35+
,
36+
},
37+
},
38+
39+
// 如需多个测试,可以复制上方元素。
40+
}
41+
42+
for _, q := range qs {
43+
a, p := q.ans, q.para
44+
fmt.Printf("~~%v~~\n", p)
45+
46+
ast.Equal(a.one, lengthOfLastWord(p. ), "输入:%v", p)
47+
}
48+
}

0 commit comments

Comments
 (0)