1 Star 1 Fork 0

子安/note_lab

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
10.string_match.cpp 1.05 KB
一键复制 编辑 原始数据 按行查看 历史
子安 提交于 2024-11-11 09:45 +08:00 . 添加文件
//
// Created by andrew on 2024/9/28.
//
#include <string>
#include <vector>
#include <unordered_set>
#include <iostream>
#include <gtest/gtest.h>
#include <climits>
using namespace std;
class Solution {
public:
bool isMatch(const std::string& s, const std::string& p) {
int m = s.length(), n = p.length();
std::vector<std::vector<bool>> dp(m + 1, std::vector<bool>(n + 1, false));
dp[0][0] = true;
for (int j = 2; j <= n; ++j) {
dp[0][j] = dp[0][j - 2] && p[j - 1] == '*';
}
for (int i = 1; i <= m; ++i) {
for (int j = 1; j <= n; ++j) {
if (p[j - 1] == '.' || s[i - 1] == p[j - 1]) {
dp[i][j] = dp[i - 1][j - 1];
} else if (p[j - 1] == '*') {
dp[i][j] = dp[i][j - 2] || (dp[i - 1][j] && (s[i - 1] == p[j - 2] || p[j - 2] == '.'));
}
}
}
return dp[m][n];
}
};
TEST(leetcode9, leetcode) {
Solution solution;
EXPECT_EQ(true, solution.isMatch("234", "234"));
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/andrewgithub/note_lab.git
git@gitee.com:andrewgithub/note_lab.git
andrewgithub
note_lab
note_lab
main

搜索帮助