1. 字符串相乘
题目链接:https://leetcode.cn/problems/multiply-strings/
给定两个以字符串形式表示的非负整数 num1 和 num2,返回 num1 和 num2 的乘积,它们的乘积也表示为字符串形式。
class Solution:
def multiply(self, num1: str, num2: str) -> str:
if num1 == "0" or num2 == "0":
return "0"
m, n = len(num1), len(num2)
res = [0] * (m + n)
for i in range(m - 1, -1, -1):
for j in range(n - 1, -1, -1):
mul = int(num1[i]) * int(num2[j])
p1, p2 = i + j, i + j + 1
total = mul + res[p2]
res[p2] = total % 10
res[p1] += total // 10
result = ""
for num in res:
if not (result == "" and num == 0):
result += str(num)
return result
2. 通配符匹配
题目链接:https://leetcode.cn/problems/wildcard-matching/
给定一个输入字符串 (s) 和一个字符模式 (p) ,请实现一个支持 '?' 和 '*' 匹配规则的通配符匹配:
- '?' 可以匹配任何单个字符。
- '*' 可以匹配任意字符序列(包括空字符序列)。
class Solution:
def isMatch(self, s: str, p: str) -> bool:
m, n = len(s), len(p)
dp = [[False] * (n + 1) for _ in range(m + 1)]
dp[0][0] = True
for j in range(1, n + 1):
if p[j - 1] == '*':
dp[0][j] = dp[0][j - 1]
for i in range(1, m + 1):
for j in range(1, n + 1):
if p[j - 1] == '*':
dp[i][j] = dp[i - 1][j] or dp[i][j - 1]
elif p[j - 1] == '?' or s[i - 1] == p[j - 1]:
dp[i][j] = dp[i - 1][j - 1]
return dp[m][n]
标签:num1,num2,res,Study,Part22,range,Algorithms,str,dp
From: https://www.cnblogs.com/stephenxiong001/p/18399405